# TCP vs UDP — Three-Way Handshake, Reliability, Flow and Congestion Control

Source: https://skills101.in/learn/computer-networks/tcp-vs-udp — Skills101 free tutorial (Computer Networks). Updated 2026-11-09.

Both **TCP** and **UDP** carry data between applications using **port numbers**. The difference is how much they promise.

![TCP vs UDP table: TCP is connection-based, reliable, ordered, slower, with a 20+ byte header, used by web and email; UDP is connectionless, unreliable, unordered, fast, 8-byte header, used by video calls, games and DNS](https://skills101.in/learn-img/computer-networks/tcp-udp.svg)

## 1. TCP: reliable, ordered delivery

### The three-way handshake

![Client sends SYN (seq = x); server replies SYN-ACK (seq = y, ack = x+1); client sends ACK (ack = y+1); the connection is established](https://skills101.in/learn-img/computer-networks/handshake.svg)

1. **SYN** — client: "I want to talk; my starting sequence number is x."
2. **SYN-ACK** — server: "OK; mine is y; I got yours (ack x+1)."
3. **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.


Practice quiz and free certificate: https://skills101.in/learn/computer-networks/tcp-vs-udp