A Visual Guide to TCP, UDP, Flow Control, and Congestion Control
This article explains TCP and UDP fundamentals, detailing TCP header fields, flow‑control and congestion‑control mechanisms, the three‑way handshake and four‑way termination, UDP’s connectionless nature, and a side‑by‑side comparison of the two protocols.
TCP Header
Source port
Destination port
32‑bit sequence number
32‑bit acknowledgment number
Header length (in 4‑byte units, default 5 → 20 bytes)
6 reserved bits
6 control bits: SYN, ACK, FIN, PUSH, URG, RST
16‑bit window size (receiver‑advertised flow‑control window; a zero window triggers a probe)
16‑bit checksum (covers header, data, and padding; computed by sender, verified by receiver)
16‑bit urgent pointer (valid only when URG=1)
Flow Control
TCP uses a sliding‑window mechanism: the receiver advertises its window size and the sender limits its sending window to the minimum of its own congestion window and the receiver’s window.
Sending window size = Math.min(congestion_window, receiver_window) Retransmission timer
Persist timer is started when a zero‑window notification is received; on timeout a zero‑window probe is sent.
Even with a zero window the receiver can accept probes, ACKs, and urgent data.
Congestion Control
Assumptions for the analysis:
Data flows in one direction; acknowledgments travel opposite.
The receiver has ample buffer space; the sender’s window is limited by network congestion.
Units are in maximum‑segment‑size (MSS) packets.
Slow Start + Congestion Avoidance
Key terms: MSS, ssthresh (slow‑start threshold), cwnd (congestion window), swnd (send window), rtt (round‑trip time).
Fast Retransmit
In the combined slow‑start and congestion‑avoidance algorithm, resetting cwnd to 1 after a fast retransmit reduces throughput.
Three Duplicate ACKs
The receiver gets an out‑of‑order segment and sends a duplicate ACK.
The sender receives three consecutive duplicate ACKs and retransmits the missing segment immediately.
Fast Recovery
Combined Algorithm
Three‑Way Handshake
Client → Server: SYN=1, seq=x
Server → Client: SYN=1, ACK=1, ack=x+1, seq=y
Client → Server: ACK=1, ack=y+1, seq=x+1
SYN segments cannot carry data but consume one sequence number.
Pure ACK segments without data do not consume a sequence number.
Four‑Way Termination
Client → Server: FIN=1, ACK=1, seq=u, ack=v (u = last byte sent + 1, v = last byte received + 1)
Server → Client: ACK=1, ack=u+1, seq=v
Server → Client: FIN=1, ACK=1, ack=u+1, seq=w (w may carry data in half‑close)
Client → Server: ACK=1, ack=w+1, seq=u+1
FIN segments consume one sequence number even without data.
Maximum Segment Lifetime (MSL) is recommended to be 2 minutes; waiting prevents a lost FIN‑ACK from causing indefinite retransmission.
Half‑Open Queue
After a server receives a SYN, it enters SYN‑RCVD state and places the connection in a half‑open queue until the three‑way handshake completes.
SYN Flood Attack
The server allocates resources on the second handshake, while the client allocates resources after the third handshake. An attacker can send many forged SYN packets, filling the half‑open queue and causing legitimate connections to be dropped, leading to DoS/DDoS.
TCP Guarantees of Data Correctness
Error control: 16‑bit checksum computed over header and data; mismatched checksum causes the segment to be discarded.
Sequencing: each segment is numbered; the receiver reorders segments before delivering to the application.
Acknowledgment & timeout retransmission: sender starts a timer after sending a segment; if ACK is not received before timeout, the segment is retransmitted.
Flow control: receiver advertises a window; sender limits transmission to avoid overflow.
Congestion control: sender reduces sending rate when network congestion is detected, using slow start, congestion avoidance, fast retransmit, and fast recovery.
UDP Header
UDP header is fixed at 8 bytes.
TCP/UDP Comparison
Connection orientation: UDP is connectionless; TCP is connection‑oriented (three‑way handshake, four‑way termination).
Broadcast/Multicast: UDP supports one‑to‑one, one‑to‑many, many‑to‑one, many‑to‑many; TCP supports only one‑to‑one.
Application‑layer handling: UDP delivers each datagram as received (message‑oriented); TCP provides a reliable byte‑stream (stream‑oriented).
Reliability: UDP offers unreliable service suitable for real‑time applications (e.g., VoIP, video conferencing); TCP offers reliable service suitable for file transfer and other reliability‑critical applications.
Header overhead: UDP header = 8 bytes; TCP header = 20 bytes minimum, up to 60 bytes with options.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
