Understanding TCP & UDP: Flow Control, Congestion Control, and Handshakes Explained
This article provides a comprehensive, illustrated guide to TCP and UDP, covering the TCP header fields, flow‑control and congestion‑control mechanisms, the three‑way handshake and four‑way termination process, as well as a side‑by‑side comparison of the two protocols.
TCP Overview
TCP Header fields
Flow control
Congestion control
Three‑way handshake and four‑way termination
Data correctness mechanisms
TCP Header
The header consists of:
Source port and destination port
32‑bit sequence number
32‑bit acknowledgment number
Header length (in 4‑byte units, default 5 → 20 bytes)
6 reserved bits
Six control flags: SYN, ACK, FIN, PUSH, URG, RST
16‑bit window size (receiver’s advertised flow‑control window)
16‑bit checksum (covers header, data and padding)
16‑bit urgent pointer (valid only when URG=1)
The checksum is computed by the sender and verified by the receiver; a mismatch causes the segment to be discarded.
Flow Control
The receiver advertises a window size (rwnd). The sender’s effective sending window is the minimum of its own congestion window (cwnd) and the advertised window: send_window = min(cwnd, rwnd) If the advertised window is zero, the sender starts a persistent timer. When the timer expires, a zero‑window probe is sent to test whether the receiver’s window has opened.
Congestion Control
TCP adjusts its sending rate based on network congestion using the following variables:
MSS – maximum segment size
ssthresh – slow‑start threshold
cwnd – congestion window
swnd – sending window (as above)
RTT – round‑trip time
The algorithm proceeds through four phases:
Slow start : cwnd starts at one MSS and grows exponentially (cwnd += MSS) each RTT until cwnd ≥ ssthresh.
Congestion avoidance : cwnd grows linearly (cwnd += MSS × MSS / cwnd) each RTT.
Fast retransmit : after receiving three duplicate ACKs, the sender immediately retransmits the missing segment without waiting for a timeout.
Fast recovery : combines fast retransmit with congestion avoidance; cwnd is set to ssthresh + 3 × MSS, then increased by MSS for each additional duplicate ACK.
When packet loss is detected via timeout, cwnd is reduced to one MSS and ssthresh is set to half of the cwnd value before loss.
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 ACKs without data do not consume a sequence number.
Four‑Way Termination
Client → Server: FIN=1, ACK=1, seq=u, ack=v Server → Client: ACK=1, ack=u+1, seq=v Server → Client: FIN=1, ACK=1, ack=u+1, seq=w Client → Server: ACK=1, ack=w+1, seq=u+1 FIN also consumes a sequence number. TCP waits for 2 × MSL (Maximum Segment Lifetime, typically 2 minutes) before fully closing to ensure delayed FINs do not cause stray retransmissions.
SYN Flood Attack
During the SYN‑RCVD state the server allocates resources for the half‑open connection. A flood of spoofed SYN packets fills the half‑open (SYN) queue, causing legitimate connection attempts to be dropped and leading to denial‑of‑service.
Data Correctness in TCP
Reliability is achieved through:
Checksum : error detection for header and payload.
Sequencing : each byte is numbered; the receiver reorders out‑of‑order segments.
Acknowledgments + timeout retransmission : the sender starts a timer for each segment; if an ACK is not received before timeout, the segment is retransmitted.
Flow control (as described above) to prevent buffer overflow.
Congestion control (as described above) to avoid network overload.
UDP Overview
UDP is a connectionless, unreliable protocol with an 8‑byte header. It provides no flow control, congestion control, or reliability guarantees; datagrams are delivered directly to the application.
TCP vs UDP Comparison
Connection orientation : TCP is connection‑oriented (requires three‑way handshake and four‑way termination); UDP is connectionless.
Broadcast/Multicast support : UDP supports one‑to‑many and many‑to‑many communication; TCP supports only one‑to‑one.
Application‑layer handling : UDP is message‑oriented (preserves datagram boundaries); TCP is byte‑stream oriented.
Reliability : UDP offers best‑effort delivery; TCP provides reliable, ordered delivery using checksum, sequencing, ACKs, and retransmission.
Header overhead : UDP header is fixed at 8 bytes; TCP header ranges from 20 bytes (no options) to 60 bytes (with options).
These differences guide protocol selection for specific application requirements.
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.
