Background
- Time: Dec 2025
- Context: Group course project for SUSTech CS305 (Computer Networks).
- Repository: Tsurumalu/Computer-Network-Project-P2P
Goal
Build a reliable peer-to-peer (P2P) file transfer application on top of UDP. All transport semantics—handshaking, reliable delivery, retransmission, and congestion control—are implemented at the application layer. The system uses a network simulator for testing under packet loss, varying topologies, and peer crashes.
System Overview
- File & chunk model: Files are split into 512 KiB chunks; each chunk is identified by a SHA-1 hash (20 bytes).
- Peer model: Each peer holds a fragment (subset) of chunks and can upload to or download from other peers concurrently.
- Download flow: On a
DOWNLOADcommand, a peer discovers missing chunks viaWHOHAS/IHAVE, requests them withGET, then reassembles completed chunks into a pickle-serialized fragment file. - Concurrency: Single-threaded event loop using
select; multiple chunk downloads from different peers run in parallel, with per-session upload limits enforced viaDENIED.
Packet Protocol
Custom UDP packets (max 1400 bytes) with header fields for type, length, and sequence/ACK number:
| Type | Code | Role |
|---|---|---|
| WHOHAS | 0 | Query which peers hold requested chunks |
| IHAVE | 1 | Reply listing available chunks |
| GET | 2 | Request a specific chunk |
| DATA | 3 | Transfer chunk payload segments |
| ACK | 4 | Acknowledge received segments |
| DENIED | 5 | Reject when upload connection limit is reached |
Implementation Highlights
- Reliable data transfer (RDT): Per-packet sequence numbers and ACKs; dynamic timeout from RTT estimation (α = 0.15, β = 0.3); timeout-based and fast retransmit (3 duplicate ACKs).
- Congestion control (Tahoe-style): Slow start and congestion avoidance with
cwndandssthresh; loss triggers window halving and return to slow start.cwndhistory is recorded and plotted with matplotlib for presentation. - Structured codebase: Refactored
peer.pywithPeerState,DownloadSession,UploadSession, and aPeerclass driving a non-blocking I/O loop. - Robustness: Handles concurrent multi-peer downloads, connection limits, peer crashes, and out-of-order segment buffering.
- Testing: Validated with course-provided pytest suites (handshaking, basic transfer, concurrency, crash scenarios, and advanced topology tests) via
grader.pyand the network simulator.
Tech Stack
- Python 3.12
- UDP (
socket) withselectfor single-threaded concurrency - SHA-1 chunk hashing
- matplotlib (congestion window visualization)
- pytest & network simulator (grading and integration tests)
- Linux / WSL (required runtime environment)