Transport Layer — TCP & UDP
756 words
4 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Transport Layer — TCP & UDP ## 🎯 Learning Objectives - Trace the TCP 3-way handshake and connection termination - Explain flow control using sliding window protocol - Describe congestion control (AIMD, slow start, fast retransmit) - Compare TCP with UDP * * * ## 1. TCP Header **Key Fields:** Field Size Purpose So...

Transport Layer — TCP & UDP
🎯 Learning Objectives
- Trace the TCP 3-way handshake and connection termination
- Explain flow control using sliding window protocol
- Describe congestion control (AIMD, slow start, fast retransmit)
- Compare TCP with UDP
1. TCP Header
pseudo0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Port | Destination Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgment Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | |C|E|U|A|P|R|S|F| | | Offset| Rsrvd |W|C|R|C|S|S|Y|I| Window | | | |R|E|G|K|H|T|N|N| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum | Urgent Pointer | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Key Fields:
| Field | Size | Purpose |
|---|---|---|
| Source/Dest Port | 16 bits each | Identify applications |
| Sequence Number | 32 bits | Byte stream position |
| ACK Number | 32 bits | Next expected byte |
| Flags (SYN, ACK, FIN, RST, PSH, URG) | 6 bits | Control |
| Window Size | 16 bits | Flow control (bytes receiver can accept) |
| Checksum | 16 bits | Error detection |
2. TCP Connection Management
2.1 3-Way Handshake
(Diagram)
Tracing:
| Step | Client State | Server State | Event |
|---|---|---|---|
| 0 | CLOSED | LISTEN | Server waiting |
| 1 | SYN_SENT | LISTEN | Client sends SYN(seq=100) |
| 2 | SYN_SENT | SYN_RCVD | Server replies SYN(seq=200)+ACK(101) |
| 3 | ESTABLISHED | SYN_RCVD | Client sends ACK(201) |
| 4 | ESTABLISHED | ESTABLISHED | Connection open! |
2.2 Connection Termination (4-Way Handshake)
(Diagram)
3. Flow Control (Sliding Window)
3.1 Intuition
The receiver tells the sender how much data it can accept via the Window field. This prevents the sender from overwhelming a slow receiver.
(Diagram)
3.2 Window Scaling
The 16-bit window field limits the window to 64KB. TCP's window scaling option multiplies the window value by a shift factor, allowing windows up to 1GB.
4. Congestion Control
4.1 Intuition
Flow control prevents overwhelming the receiver. Congestion control prevents overwhelming the network. TCP uses several algorithms to detect and react to congestion.
4.2 Congestion Window Phases
(Diagram)
4.3 AIMD Example
cwnd evolution:
pseudoTime (RTT) | cwnd (MSS) | Event -----------+------------+------- 0 | 1 | Slow start begins 1 | 2 | 2 | 4 | 3 | 8 | ssthresh = 16 4 | 16 | 5 | 32 | Packet loss (3 dup ACKs) | | ssthresh = 16, cwnd = 16 6 | 17 | Congestion avoidance 7 | 18 | ... 10 | 32 | Loss again | | ssthresh = 16, cwnd = 16
4.4 TCP Tahoe vs Reno vs Cubic
| Version | Slow Start | Fast Recovery | Loss Behavior |
|---|---|---|---|
| Tahoe | Yes | No | Timeout → cwnd=1; 3 dup ACKs → cwnd=1 + ssthresh |
| Reno | Yes | Yes | 3 dup ACKs → fast recovery (cwnd=havessthresh) |
| Cubic | Yes | Yes (advanced) | Better for high-bandwidth, long-distance links |
5. TCP vs UDP
| Property | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (handshake) | Connectionless |
| Reliability | Guaranteed delivery, in-order | Best-effort, unordered |
| Flow control | Yes (sliding window) | No |
| Congestion control | Yes (AIMD) | No |
| Header size | 20-60 bytes | 8 bytes |
| Uses | Web (HTTP), Email (SMTP), File transfer (FTP) | DNS, VoIP, Streaming, DHCP |
6. 📝 Practice Questions
Q1: During the 3-way handshake, what does each party learn from the SYN and ACK?Answer:
- Client learns: server is alive, server's initial sequence number, server received client's SYN
- Server learns: client is alive, client's initial sequence number, client received server's SYN+ACK Both learn each other's initial sequence numbers for reliable communication. Q2: What happens to TCP throughput when the window size is small?
Answer: Throughput ≤ WindowSize / RTT. If WindowSize is small compared to the bandwidth-delay product, the link is underutilized. For high-latency links (satellite, transcontinental), window scaling is essential. Q3: Explain the difference between flow control and congestion control.Answer: Flow control prevents the sender from overwhelming the receiver (receiver-driven, window field in ACKs). Congestion control prevents the sender from overwhelming the network (sender-driven, cwnd adjustment based on inferred packet loss). Q4: Why does TCP use a 3-way handshake instead of a 2-way handshake?Answer: A 2-way handshake can't prevent duplicate connections from delayed SYN packets. With 3-way handshake, the server doesn't allocate resources until it receives the client's ACK, preventing half-open connections from old SYNs. Q5: What is the purpose of TCP's TIME_WAIT state?Answer: TIME_WAIT (duration: 2×MSL ≈ 2 minutes) ensures that: (1) the final ACK reaches the server (retransmitted if lost), (2) any delayed packets from this connection are discarded before a new connection with the same socket pair is created.
7. 🔗 Cross-References
- Week 1 - Application Layer: How applications use TCP (HTTP, SMTP, FTP)
- Week 3 - Network Layer: IP routing delivers TCP segments
- BSCS4021 (Advanced Algorithms): Network flow, congestion algorithms Join Discord PreviousOSI & TCP/IP ModelsNextIP Addressing & Routing