Fundamentals 15 min read

TCP & UDP Deep Dive: Flow Control, Congestion Control & Handshakes Explained

This illustrated guide explains TCP and UDP protocols, covering TCP header structure, flow control via sliding windows, congestion control algorithms (slow start, congestion avoidance, fast retransmit/recovery), three-way handshake and four-way handshake mechanics with sequence numbers, SYN flood attacks, and a detailed TCP vs UDP comparison across connection model, reliability, and header overhead.

Linux Tech Enthusiast
Linux Tech Enthusiast
Linux Tech Enthusiast
TCP & UDP Deep Dive: Flow Control, Congestion Control & Handshakes Explained

TCP Protocol Deep Dive

1. TCP Header Fields

The TCP header contains the following key fields: Source Port (16 bits) Destination Port (16 bits) Sequence Number (32 bits) Acknowledgment Number (32 bits) Header Length (4 bits, unit = 4 bytes, default 5 → 20 bytes) Reserved (6 bits) Control Flags (6 bits): SYN, ACK, FIN, PUSH, URG,

RST
Window Size

(16 bits) – receiver window for flow control; zero window triggers probe Checksum (16 bits) – covers header, data, padding; verified at both ends Urgent Pointer (16 bits) – valid only when URG =1; marks end of urgent data

TCP header diagram
TCP header diagram

2. Flow Control

Flow control ensures the sender does not overwhelm the receiver. It uses a sliding window mechanism:

The receiver advertises its receive window (rwnd) in the TCP header.

The sender's effective window = min(cwnd, rwnd).

When rwnd = 0, the sender starts a persistent timer and sends zero-window probe segments.

Even with zero window, the receiver must accept: zero-window probes, pure ACKs, and segments carrying urgent data.

3. Congestion Control

Congestion control reduces the sending rate when the network is congested. Assumptions: unidirectional data flow, receiver buffer always sufficient, units in MSS (Maximum Segment Size).

Key Variables

MSS

– Maximum Segment Size ssthresh – slow-start threshold cwnd – congestion window swnd – send window =

min(cwnd, rwnd)
RTT

– round-trip time

Congestion control assumptions
Congestion control assumptions

Slow Start + Congestion Avoidance

Initially cwnd = 1 MSS; each ACK increases cwnd by 1 MSS (exponential growth).

When cwnd ≥ ssthresh, switch to congestion avoidance : increase cwnd by 1 MSS per RTT (linear growth).

On timeout: ssthresh = cwnd/2, cwnd = 1, restart slow start.

Slow start and congestion avoidance graph
Slow start and congestion avoidance graph

Fast Retransmit

Receiver sends duplicate ACKs for out-of-order segments.

Sender receives 3 duplicate ACKs → assumes segment loss → retransmits immediately without waiting for timeout.

Fast retransmit illustration
Fast retransmit illustration

Fast Recovery

After fast retransmit: ssthresh = cwnd/2, cwnd = ssthresh + 3 MSS (for the 3 dup ACKs).

Each additional dup ACK increments cwnd by 1 MSS.

When new ACK arrives: cwnd = ssthresh, enter congestion avoidance.

Fast recovery combined graph
Fast recovery combined graph

4. Connection Establishment & Termination

4.1 Three-Way Handshake

Client → Server: SYN=1, seq=x Server → Client: ACK=1, ack=x+1, SYN=1, seq=y Client → Server: ACK=1, ack=y+1, seq=x+1 Rules: SYN=1 segments cannot carry data but consume one sequence number. Pure ACKs without data do not consume sequence numbers.

Three-way handshake diagram
Three-way handshake diagram

4.2 Four-Way Handshake (Connection Termination)

Active closer → Peer: FIN=1, ACK=1, seq=u, ack=v Peer → Active closer: ACK=1, ack=u+1, seq=v Peer → Active closer: FIN=1, ACK=1, ack=u+1, seq=w (after sending remaining data)

Active closer → Peer: ACK=1, ack=w+1, seq=u+1 Rule: FIN=1 consumes one sequence number even without data. The active closer then enters TIME_WAIT for 2×MSL (MSL = Maximum Segment Lifetime, recommended 2 minutes) to handle possible lost final ACK.

Why 2 MSL? If the final ACK is lost, the peer will retransmit FIN; the 2 MSL wait ensures the active closer can respond.
Four-way handshake diagram
Four-way handshake diagram

4.3 Keepalive Timer

Detects dead peers on idle connections. If no data for a period, send probe; after several unanswered probes, close connection.

Keepalive timer illustration
Keepalive timer illustration

4.4 Half-Connection Queue

When server receives SYN, it enters SYN_RCVD and places the request in a half-connection queue until the handshake completes.

4.5 Why Not Two-Way Handshake?

Delayed duplicate SYN could arrive after the original connection is closed, causing the server to allocate resources for a non-existent connection.

Two-way handshake problem
Two-way handshake problem

4.6 Why Not Three-Way Termination?

The passive closer may still have data to send; it needs to send its own FIN after finishing, requiring four segments.

4.7 SYN Flood Attack

Server allocates resources at the second handshake (upon receiving SYN), while client allocates at the third. Attacker sends many SYN packets from spoofed IPs; server replies SYN+ACK and waits, filling the half-connection queue, dropping legitimate requests. This is a classic DoS/DDoS attack.

5. How TCP Guarantees Data Correctness

Error Control : Checksum (one's complement sum of header + data + padding). Mismatch → discard segment, no ACK.

Sequencing & Ordering : Every byte numbered; receiver reorders before delivering to application.

ACK + Timeout Retransmission : Sender starts timer per segment; on timeout, retransmits.

Flow Control : Sliding window based on receiver buffer.

Congestion Control : Slow start, congestion avoidance, fast retransmit, fast recovery.

UDP Protocol

UDP header is fixed 8 bytes : source port, destination port, length, checksum.

UDP header diagram
UDP header diagram

TCP vs UDP Comparison

TCP/IP protocol stack
TCP/IP protocol stack

1. Connection Model

UDP : Connectionless.

TCP : Connection-oriented (three-way handshake, four-way handshake).

Connection model comparison
Connection model comparison

2. Broadcast & Multicast

UDP : Supports one-to-one, one-to-many, many-to-one, many-to-many.

TCP : Only one-to-one.

Broadcast multicast comparison
Broadcast multicast comparison

3. Application Data Handling

UDP : Message-oriented – each application write becomes one UDP datagram.

TCP : Byte-stream – application writes are buffered and segmented by TCP; enables reliability, flow control, congestion control.

Message vs byte stream
Message vs byte stream

4. Reliability

UDP : Unreliable, no guarantee of delivery, order, or duplicate protection. Suitable for real-time apps (VoIP, video conferencing).

TCP : Reliable, in-order, duplicate-free delivery. Suitable for file transfer, email, web.

Reliability comparison
Reliability comparison

5. Header Overhead

UDP : 8 bytes fixed.

TCP : Minimum 20 bytes, maximum 60 bytes (with options).

Header overhead comparison
Header overhead comparison
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPnetwork protocolsfour-way handshakethree-way handshakeflow controlcongestion controlUDPSYN flood
Linux Tech Enthusiast
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.