Sub topic 3
TCP and UDP: which protocols use which
This chapter assumes you know what a port number identifies and the three port ranges. It also assumes you can run short Python scripts and terminal commands. Here you learn how TCP and UDP differ, and why each application protocol uses one, the other, or both.
Two transport protocols
TCP and UDP both carry application data between ports. They make different promises.
| Property | TCP | UDP |
|---|---|---|
| Connection | Handshake before data | None, each datagram stands alone |
| Delivery | Retransmits lost segments | No retransmission |
| Ordering | Bytes arrive in order | Datagrams may arrive out of order |
| Message boundaries | None, a continuous byte stream | Preserved, one send is one datagram |
| Overhead | Higher (state, acknowledgements) | Lower (8-byte header) |
TCP and UDP port spaces are separate. TCP port 53 and UDP port 53 are different endpoints, and a service can listen on one without the other.
How protocols choose
A protocol picks TCP when it needs every byte, in order. It picks UDP when it needs low latency, small exchanges, or broadcast and multicast, which TCP cannot do.
| Protocol | Port | Transport | Reason |
|---|---|---|---|
| HTTP / HTTPS | 80 / 443 | TCP | Reliable bulk transfer |
| SSH | 22 | TCP | Ordered, reliable session |
| SMTP | 25 | TCP | Messages must not be lost |
| FTP | 21 | TCP | Reliable file transfer |
| DNS | 53 | UDP and TCP | Small queries use UDP, large responses and zone transfers use TCP |
| DHCP | 67, 68 | UDP | Clients have no IP address yet and must broadcast |
| NTP | 123 | UDP | Small, time-sensitive packets |
| TFTP | 69 | UDP | Minimal design for simple boot loaders |
| SNMP | 161 | UDP | Short request and response polls |
| Syslog | 514 | UDP (commonly) | Low-cost log forwarding |
HTTP/3 is a modern exception. It runs over QUIC, which uses UDP port 443 and builds its own reliability on top.
Seeing the difference in code
The Python socket module lets you look up the transport for a service. The lookup fails when the services database has no entry for that name and protocol.
import socket
for name, proto in (("http", "tcp"), ("domain", "tcp"), ("domain", "udp"), ("ntp", "udp")):
try:
print(name, proto, socket.getservbyname(name, proto))
except OSError:
print(name, proto, "not in services database")Both domain lookups return 53. That shows the same number is registered for two transports.
Common errors
- Opening only TCP 53 in a firewall. Most DNS queries use UDP and will time out.
- Testing a UDP service with
nc -vz. Without the-uflag,nctests TCP only. Even with-u, a UDP "success" often means only that no ICMP error came back. - Assuming UDP is always faster for the whole task. If the application must rebuild reliability itself, the savings can vanish.
Exercise
A colleague opens TCP port 123 on a firewall so that servers can synchronize their clocks, but synchronization still fails. What is the likely cause?
Show answer
NTP uses UDP port 123, not TCP. The rule must allow UDP 123. TCP and UDP ports are separate, so opening the TCP port does not help.
Exercise
Why does DHCP use UDP rather than TCP?
Show answer
A DHCP client has no IP address when it starts and must broadcast its request. TCP needs an established connection between two known hosts and cannot broadcast, so UDP is required.