TCP — Reliable Transport Layer

UDP sends packets and hopes for the best. TCP (Transmission Control Protocol) guarantees delivery, ordering, and correctness — at the cost of overhead. It's the backbone of HTTP, SSH, email, and most web traffic.

What TCP Provides

GuaranteeHow
Reliable deliveryAcknowledgements + retransmission
In-order deliverySequence numbers; reorder at receiver
No duplicatesTrack received sequence numbers
Error detectionChecksum in every segment
Flow controlReceiver tells sender how much it can handle
Congestion controlSlow down when the network is stressed

TCP Segment Structure

TCP Header (20+ bytes)Source Port16 bitsDestination Port16 bitsSequence Number32 bitsAcknowledgement Number32 bitsOffset4 bitsReserved3 bitsFlags (SYN/ACK/FIN…)9 bitsWindow Size16 bitsChecksum16 bitsUrgent Pointer16 bits

Key fields:

  • Sequence number: byte offset of the first data byte in this segment
  • ACK number: next expected byte from the other side
  • Flags: SYN, ACK, FIN, RST, PSH, URG
  • Window size: how many bytes the receiver is willing to accept (flow control)

Three-Way Handshake

TCP requires a connection before data flows:

ClientServerSYN (seq=x)"Let's connect, I'll start from seq x"SYN-ACK (seq=y, ack=x+1)"OK, my seq is y, I got your x"ACK (ack=y+1)"Got it — data flows now"

After the handshake, both sides have agreed on initial sequence numbers and are ready to send data.

Reliability: ACKs and Retransmission

For every segment received, the receiver sends an acknowledgement (ACK) with the next expected byte number.

text
Loading...

Retransmission timeout (RTO): dynamically estimated based on measured round-trip time (RTT). TCP tracks SRTT (smoothed RTT) and adjusts the RTO accordingly.

Fast Retransmit: if the sender receives 3 duplicate ACKs for the same sequence number, it retransmits immediately without waiting for timeout — a sign that a segment was lost but later ones arrived.

Flow Control — Sliding Window

The receiver controls how much data the sender can have in-flight using the window size field.

text
Loading...

If the receiver's buffer fills up (app not reading fast enough), it advertises window=0. The sender stops and polls periodically.

Congestion Control

Flow control is about the receiver's capacity. Congestion control is about the network's capacity — avoiding overloading routers between sender and receiver.

Slow Start

TCP starts cautiously and grows exponentially:

text
Loading...

Congestion Avoidance (AIMD)

After reaching ssthresh, TCP grows linearly (+1 per RTT, not per ACK):

text
Loading...

On Loss (Timeout)

  • Reset cwnd to 1
  • ssthresh = cwnd/2

On 3 Duplicate ACKs (Fast Recovery)

  • ssthresh = cwnd/2
  • cwnd = ssthresh + 3 (less drastic than timeout)

This is the classic TCP Reno / TCP CUBIC algorithm.

Connection Termination — Four-Way Handshake

Closing is a four-step process (each side closes independently):

ClientServerFIN"I'm done sending"ACK"Got your FIN"FIN"I'm done too"ACK"Got it"TIME_WAIT(2×MSL ≈ 60–120s)

TIME_WAIT ensures the final ACK arrived and lets old duplicate packets expire.

Python: TCP Sockets in Practice

python
Loading...

Important: TCP is a byte stream, not a message protocol. A single send() might be received in multiple recv() calls, or multiple send() calls might arrive in one recv(). Application-level framing (length prefix, delimiter, HTTP headers) handles this.

TCP vs UDP Summary

TCPUDP
ConnectionRequired (3-way handshake)None
DeliveryGuaranteed + orderedBest-effort
Congestion controlYesNo
Overhead per segment20+ bytes header8 bytes header
LatencyHigherLower
ThroughputHigh, but limited by RTTPotentially higher for bulk
Use casesHTTP, SSH, database connectionsDNS, video streaming, gaming

Key Takeaways

  • TCP's three-way handshake establishes sequence numbers before data flows
  • Reliability comes from ACKs + sequence numbers + retransmission on timeout
  • Flow control (receiver window) prevents overwhelming the receiver's buffer
  • Congestion control (slow start + AIMD) prevents overwhelming the network
  • TCP is a stream protocol — application code must handle message framing