Sub topic 3
UDP: A Minimal Transport Service
Prerequisites and working model
This chapter assumes you know the layered network model, IP addresses, ports, bandwidth, latency, and round-trip time (RTT). You should also know basic Python socket programming.
The User Datagram Protocol (UDP) is a transport-layer protocol. It lets an application send short messages called datagrams between programs. UDP adds port numbers and an error-detection checksum, but it does not establish a connection or guarantee delivery.
What UDP provides
An IP address identifies a host. A port identifies an application endpoint on that host. A UDP communication is therefore associated with source and destination IP addresses and ports.
UDP has a fixed 8-byte header:
| Field | Size | Purpose |
|---|---|---|
| Source port | 16 bits | Port of the sending application |
| Destination port | 16 bits | Port of the receiving application |
| Length | 16 bits | Header plus data length in bytes |
| Checksum | 16 bits | Detects corruption |
The UDP length includes both the 8-byte header and the payload. Therefore, if a UDP datagram has a 100-byte payload, its length field is 108 bytes.
UDP preserves message boundaries. If the sender sends three datagrams, the receiver reads three separate datagrams. This differs from TCP, which presents a continuous byte stream.
import socket
import sys
HOST = "127.0.0.1"
PORT = 5000
if len(sys.argv) != 2 or sys.argv[1] not in {"server", "client"}:
raise SystemExit("Usage: python udp_demo.py server|client")
if sys.argv[1] == "server":
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.bind((HOST, PORT))
print(f"Listening on {HOST}:{PORT}")
data, address = sock.recvfrom(1024)
print(f"Received {data!r} from {address}")
sock.sendto(data, address)
else:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.sendto(b"hello over UDP", (HOST, PORT))
data, address = sock.recvfrom(1024)
print(f"Reply from {address}: {data!r}")Run the server first:
python udp_demo.py serverRun the client in another terminal:
python udp_demo.py clientThe server uses bind and recvfrom. The client uses sendto. Neither side performs a TCP-style handshake.
What UDP does not provide
UDP does not provide:
- reliable delivery;
- retransmission after loss;
- ordering;
- duplicate suppression;
- receiver flow control;
- network congestion control.
A damaged datagram is normally discarded after checksum verification. A lost datagram produces no automatic UDP response. If an application needs acknowledgements, sequence numbers, retransmissions, or rate control, it must implement them itself or use a higher-level protocol.
Common GATE traps
UDP is often chosen for DNS queries, streaming, voice, and games where low delay or application-specific recovery matters. However, sending data through UDP does not make it reliable or congestion-safe.
A checksum detects corruption; it does not recover the data. Also, UDP preserves datagram boundaries, but a datagram larger than the receiver's buffer may be truncated by the receiving API.
Exercise
A UDP payload is 512 bytes. What value appears in the UDP length field, and what happens if the datagram is lost?
Show answer
The length is 520 bytes because the 8-byte header is included. UDP does not retransmit the lost datagram; recovery must be implemented by the application.