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#
| Guarantee | How |
|---|---|
| Reliable delivery | Acknowledgements + retransmission |
| In-order delivery | Sequence numbers; reorder at receiver |
| No duplicates | Track received sequence numbers |
| Error detection | Checksum in every segment |
| Flow control | Receiver tells sender how much it can handle |
| Congestion control | Slow down when the network is stressed |
TCP Segment Structure#
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:
After the handshake, both sides have agreed on initial sequence numbers and are ready to send data.
Why three messages and not two? Think of the start of a phone call: "Can you hear me?" / "Yes! Can you hear me?" / "Yes!". Each side has to learn two things, that the other side's messages reach it and that its own messages reach the other side, and it only learns the second from an answer to something it sent. After the SYN-ACK the client knows both; the server still does not know its SYN-ACK arrived until the final ACK. The simulation below ticks off what each side knows as each segment lands, and only then lets the page through. The four-way close is covered further down this page.
Reliability: ACKs and Retransmission#
For every segment received, the receiver sends an acknowledgement (ACK) with the next expected byte number.
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. Three duplicate ACKs are 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.
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:
Congestion Avoidance (AIMD)#
After reaching ssthresh, TCP grows linearly (+1 per RTT, not per ACK):
On Loss (Timeout)#
- Reset
cwndto 1 ssthresh= cwnd/2
On 3 Duplicate ACKs (Fast Recovery)#
ssthresh= cwnd/2cwnd= ssthresh + 3 (less drastic than timeout)
This is the classic TCP Reno algorithm (AIMD + fast recovery). Modern Linux defaults to TCP CUBIC, which is a distinct algorithm that replaces the linear +1 MSS/RTT growth with a cubic window-growth function.
Connection Termination: Four-Way Handshake#
Closing is a four-step process (each side closes independently):
TIME_WAIT ensures the final ACK arrived and lets old duplicate packets expire.
Python: TCP Sockets in Practice#
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#
| TCP | UDP | |
|---|---|---|
| Connection | Required (3-way handshake) | None |
| Delivery | Guaranteed + ordered | Best-effort |
| Congestion control | Yes | No |
| Overhead per segment | 20+ bytes header | 8 bytes header |
| Latency | Higher | Lower |
| Throughput | High, but limited by RTT | Potentially higher for bulk |
| Use cases | HTTP, SSH, database connections | DNS, video streaming, gaming |
What to Remember#
- 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, so application code must handle message framing