TCP and UDP
Reliable streams versus fast datagrams, and the handshake behind every connection.
IP only delivers packets from one address to another, with no guarantees. The transport layer adds ports and, in the case of TCP, reliability.
| TCP | UDP | |
|---|---|---|
| Connection | Yes: a handshake first | No: just send |
| Delivery | Reliable and in order, with retransmission | Best effort: packets can be lost or reordered |
| Overhead | Higher | Very low |
| Used by | HTTP(S), SSH, email, databases | DNS queries, video calls, games, QUIC (HTTP/3) |
The TCP three-way handshake
- Client sends SYN ("I want to talk, my sequence starts at x").
- Server answers SYN-ACK ("Agreed, mine starts at y, I got x").
- Client sends ACK ("Got y"). The connection is established.
Only then does data flow. TLS adds its own handshake on top, which is why the first request to a new HTTPS server takes a few round trips.
What the failures look like
- Connection refused: the host replied with a RST because nothing listens on the port.
- Timeout: no reply at all. A firewall is probably dropping the packets, or the host is unreachable.
- Connection reset: the other side closed abruptly, often a proxy or a crash.
NoteTCP also adapts to congestion: it slows down when packets are lost. That is why a lossy link makes TCP transfers much slower than the raw bandwidth suggests.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.