<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>UDP on Zitao Liao</title><link>http://lzteddy.com/tags/udp/</link><description>Recent content in UDP on Zitao Liao</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 19 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="http://lzteddy.com/tags/udp/index.xml" rel="self" type="application/rss+xml"/><item><title>P2P Reliable File Transfer with Congestion Control</title><link>http://lzteddy.com/p/cs305-p2p-file-transfer/</link><pubDate>Fri, 19 Dec 2025 00:00:00 +0000</pubDate><guid>http://lzteddy.com/p/cs305-p2p-file-transfer/</guid><description>&lt;h2 id="background"&gt;Background
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Time: Dec 2025&lt;/li&gt;
&lt;li&gt;Context: Group course project for SUSTech CS305 (Computer Networks).&lt;/li&gt;
&lt;li&gt;Repository: &lt;a class="link" href="https://github.com/Tsurumalu/Computer-Network-Project-P2P" target="_blank" rel="noopener"
 &gt;Tsurumalu/Computer-Network-Project-P2P&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="goal"&gt;Goal
&lt;/h2&gt;&lt;p&gt;Build a &lt;strong&gt;reliable peer-to-peer (P2P) file transfer application&lt;/strong&gt; on top of &lt;strong&gt;UDP&lt;/strong&gt;. 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.&lt;/p&gt;
&lt;h2 id="system-overview"&gt;System Overview
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;File &amp;amp; chunk model:&lt;/strong&gt; Files are split into 512 KiB chunks; each chunk is identified by a SHA-1 hash (20 bytes).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Peer model:&lt;/strong&gt; Each peer holds a fragment (subset) of chunks and can upload to or download from other peers concurrently.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Download flow:&lt;/strong&gt; On a &lt;code&gt;DOWNLOAD&lt;/code&gt; command, a peer discovers missing chunks via &lt;code&gt;WHOHAS&lt;/code&gt; / &lt;code&gt;IHAVE&lt;/code&gt;, requests them with &lt;code&gt;GET&lt;/code&gt;, then reassembles completed chunks into a pickle-serialized fragment file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Concurrency:&lt;/strong&gt; Single-threaded event loop using &lt;code&gt;select&lt;/code&gt;; multiple chunk downloads from different peers run in parallel, with per-session upload limits enforced via &lt;code&gt;DENIED&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="packet-protocol"&gt;Packet Protocol
&lt;/h2&gt;&lt;p&gt;Custom UDP packets (max 1400 bytes) with header fields for type, length, and sequence/ACK number:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Type&lt;/th&gt;
 &lt;th&gt;Code&lt;/th&gt;
 &lt;th&gt;Role&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;WHOHAS&lt;/td&gt;
 &lt;td&gt;0&lt;/td&gt;
 &lt;td&gt;Query which peers hold requested chunks&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;IHAVE&lt;/td&gt;
 &lt;td&gt;1&lt;/td&gt;
 &lt;td&gt;Reply listing available chunks&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;GET&lt;/td&gt;
 &lt;td&gt;2&lt;/td&gt;
 &lt;td&gt;Request a specific chunk&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;DATA&lt;/td&gt;
 &lt;td&gt;3&lt;/td&gt;
 &lt;td&gt;Transfer chunk payload segments&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;ACK&lt;/td&gt;
 &lt;td&gt;4&lt;/td&gt;
 &lt;td&gt;Acknowledge received segments&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;DENIED&lt;/td&gt;
 &lt;td&gt;5&lt;/td&gt;
 &lt;td&gt;Reject when upload connection limit is reached&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="implementation-highlights"&gt;Implementation Highlights
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Reliable data transfer (RDT):&lt;/strong&gt; Per-packet sequence numbers and ACKs; dynamic timeout from RTT estimation (α = 0.15, β = 0.3); timeout-based and fast retransmit (3 duplicate ACKs).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Congestion control (Tahoe-style):&lt;/strong&gt; Slow start and congestion avoidance with &lt;code&gt;cwnd&lt;/code&gt; and &lt;code&gt;ssthresh&lt;/code&gt;; loss triggers window halving and return to slow start. &lt;code&gt;cwnd&lt;/code&gt; history is recorded and plotted with matplotlib for presentation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Structured codebase:&lt;/strong&gt; Refactored &lt;code&gt;peer.py&lt;/code&gt; with &lt;code&gt;PeerState&lt;/code&gt;, &lt;code&gt;DownloadSession&lt;/code&gt;, &lt;code&gt;UploadSession&lt;/code&gt;, and a &lt;code&gt;Peer&lt;/code&gt; class driving a non-blocking I/O loop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Robustness:&lt;/strong&gt; Handles concurrent multi-peer downloads, connection limits, peer crashes, and out-of-order segment buffering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Validated with course-provided pytest suites (handshaking, basic transfer, concurrency, crash scenarios, and advanced topology tests) via &lt;code&gt;grader.py&lt;/code&gt; and the network simulator.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="tech-stack"&gt;Tech Stack
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Python 3.12&lt;/li&gt;
&lt;li&gt;UDP (&lt;code&gt;socket&lt;/code&gt;) with &lt;code&gt;select&lt;/code&gt; for single-threaded concurrency&lt;/li&gt;
&lt;li&gt;SHA-1 chunk hashing&lt;/li&gt;
&lt;li&gt;matplotlib (congestion window visualization)&lt;/li&gt;
&lt;li&gt;pytest &amp;amp; network simulator (grading and integration tests)&lt;/li&gt;
&lt;li&gt;Linux / WSL (required runtime environment)&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>