Quiz 2

Transport Layer — TCP & UDP

756 words
4 min read
Python Week 1: the first filter for runtime behavior
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

pseudo
 0                   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:
FieldSizePurpose
Source/Dest Port16 bits eachIdentify applications
Sequence Number32 bitsByte stream position
ACK Number32 bitsNext expected byte
Flags (SYN, ACK, FIN, RST, PSH, URG)6 bitsControl
Window Size16 bitsFlow control (bytes receiver can accept)
Checksum16 bitsError detection

2. TCP Connection Management

2.1 3-Way Handshake

(Diagram) Tracing:
StepClient StateServer StateEvent
0CLOSEDLISTENServer waiting
1SYN_SENTLISTENClient sends SYN(seq=100)
2SYN_SENTSYN_RCVDServer replies SYN(seq=200)+ACK(101)
3ESTABLISHEDSYN_RCVDClient sends ACK(201)
4ESTABLISHEDESTABLISHEDConnection 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:
pseudo
Time (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

VersionSlow StartFast RecoveryLoss Behavior
TahoeYesNoTimeout → cwnd=1; 3 dup ACKs → cwnd=1 + ssthresh
RenoYesYes3 dup ACKs → fast recovery (cwnd=havessthresh)
CubicYesYes (advanced)Better for high-bandwidth, long-distance links

5. TCP vs UDP

PropertyTCPUDP
ConnectionConnection-oriented (handshake)Connectionless
ReliabilityGuaranteed delivery, in-orderBest-effort, unordered
Flow controlYes (sliding window)No
Congestion controlYes (AIMD)No
Header size20-60 bytes8 bytes
UsesWeb (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

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.