Compared
TCP vs UDP
TCP sets up a connection and delivers every byte in order, resending what is lost. UDP sends separate datagrams with no connection, no acknowledgement and no resend. Use TCP when every byte must arrive, and UDP when a late packet is worth less than a lost one.
Side by side
| TCP | UDP | |
|---|---|---|
| Connection | Opens with a three-way handshake (SYN, SYN-ACK, ACK) and closes with FINs | None: each datagram stands alone |
| Delivery | Every byte is numbered, acknowledged, resent when lost and delivered in order | No acknowledgement, no resend, no ordering: success only means the datagram left |
| Speed control | The receiver's window and congestion control slow the sender down | None: the program sets its own pace |
| Header | 20 bytes at least | 8 bytes |
| A closed port answers | With a TCP reset | With ICMP port unreachable, if at all |
| Used by | HTTP/1.1 and HTTP/2, SSH, SMTP, BGP | DNS, DHCP, NTP, VoIP, WireGuard, QUIC (HTTP/3) |
Each one explained
- TCP
- Reliable, ordered byte streams between programs: handshake, acknowledgements, retransmission.
- UDP
- Independent datagrams with ports and nothing else: no connection, no acknowledgement, no retries.
- Three-way handshake
- SYN, SYN-ACK, ACK: how TCP opens a connection, each side agreeing on starting sequence numbers.
- Port
- A 16-bit number that picks the program on a device: web 80 and 443, DNS 53, SSH 22.
See the difference on real machines
Each lab is a small network of real Linux machines in your browser.
- TCP: how a file survives a bad line
- UDP: why late is worse than lost
- Ports and sockets: one address, many programs
Open TCP: how a file survives a bad line