P2P Reliable File Transfer with Congestion Control

SUSTech CS305 course project implementing a UDP-based peer-to-peer file transfer system with reliable data transfer and TCP-like congestion control.

Background

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 DOWNLOAD command, a peer discovers missing chunks via WHOHAS / IHAVE, requests them with GET, 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 via DENIED.

Packet Protocol

Custom UDP packets (max 1400 bytes) with header fields for type, length, and sequence/ACK number:

TypeCodeRole
WHOHAS0Query which peers hold requested chunks
IHAVE1Reply listing available chunks
GET2Request a specific chunk
DATA3Transfer chunk payload segments
ACK4Acknowledge received segments
DENIED5Reject 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 cwnd and ssthresh; loss triggers window halving and return to slow start. cwnd history is recorded and plotted with matplotlib for presentation.
  • Structured codebase: Refactored peer.py with PeerState, DownloadSession, UploadSession, and a Peer class 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.py and the network simulator.

Tech Stack

  • Python 3.12
  • UDP (socket) with select for single-threaded concurrency
  • SHA-1 chunk hashing
  • matplotlib (congestion window visualization)
  • pytest & network simulator (grading and integration tests)
  • Linux / WSL (required runtime environment)
comments powered by Disqus