Computer Networks · Lesson 4 of 7 · Updated ·
TCP vs UDP — Three-Way Handshake, Reliability, Flow and Congestion Control
Both TCP and UDP carry data between applications using port numbers. The difference is how much they promise.
1. TCP: reliable, ordered delivery
The three-way handshake
- SYN — client: "I want to talk; my starting sequence number is x."
- SYN-ACK — server: "OK; mine is y; I got yours (ack x+1)."
- ACK — client: "Got yours (ack y+1)." → connected.
Closing uses FIN and ACK from both sides (four steps).
How TCP makes delivery reliable
- Sequence numbers — every byte is numbered, so the receiver can put segments in order.
- Acknowledgements (ACKs) — the receiver confirms what arrived.
- Retransmission — if no ACK arrives in time, the sender resends.
- Checksum — detects corrupted data.
Flow control vs congestion control
| Flow control | Congestion control | |
|---|---|---|
| Protects | the receiver from being flooded | the network from overload |
| How | receiver advertises a window (how much it can accept) | slow start, then additive increase / multiplicative decrease |
2. UDP: fast and simple
UDP just sends datagrams — no connection, no ACKs, no ordering, no retransmission. Its header is only 8 bytes.
Why would anyone want that? Because for some apps, late data is useless:
- In a video call, a frame that arrives 2 seconds late is worse than a skipped frame.
- Online games send the latest position many times a second.
- DNS queries are tiny; if one is lost, the app just asks again.
3. Which one does each app use?
| App | Protocol | Port |
|---|---|---|
| Web (HTTP / HTTPS) | TCP (HTTP/3 uses QUIC over UDP) | 80 / 443 |
| SSH | TCP | 22 |
| Email (SMTP) | TCP | 25 / 587 |
| MySQL / PostgreSQL | TCP | 3306 / 5432 |
| DNS | UDP (TCP for big answers) | 53 |
| DHCP | UDP | 67 / 68 |
| Video calls, live streaming, games | mostly UDP | various |
Port ranges: 0–1023 well-known, 1024–49151 registered, 49152–65535 dynamic (your browser's side of a connection).
4. Sockets
A socket = IP address + port. A TCP connection is identified by both ends: (client IP, client port, server IP, server port). That's how one server on port 443 handles thousands of users at once.
Common mistakes
- Saying UDP is "bad". It's the right choice for real-time traffic.
- Confusing flow control (receiver) with congestion control (network).
- Thinking the handshake has 2 steps — it's 3 (SYN, SYN-ACK, ACK).
Quick recap
- TCP: connection (3-way handshake), sequence numbers, ACKs, retransmission, flow and congestion control.
- UDP: no connection, no guarantees, small header, very fast.
- TCP for correctness (web, email, files); UDP for real-time (calls, games, DNS).
- Socket = IP + port; connections are identified by both ends.