Computer Networking › Section C · The transport layer › Topic C.3

Connectionless transport: UDP

UDP is the thinnest transport protocol the internet offers: four header fields, no handshake and no promises. That bare simplicity is exactly why DNS, live media, games and even the modern web's QUIC protocol build on it.

A bare-bones transport

Imagine designing the simplest transport protocol that still works. Passing application messages straight to IP is not enough, because the receiving host would not know which process each message is for. UDP, the User Datagram Protocol, defined in RFC 768 in 1980, adds the minimum: source and destination port numbers for multiplexing and demultiplexing, a length and a checksum. There is no handshake, so UDP is connectionless. A DNS client, for example, wraps its query in a UDP segment and sends it at once. If no answer comes back, perhaps because the query or the reply was lost, the client may try again, ask another server or report failure.

Why choose UDP over TCP?

TCP is reliable, so why would anyone give that up? First, control over timing: UDP sends each message the moment the application hands it over, while TCP may hold data back for congestion control and holds back later data while it resends a lost segment. Real-time applications prefer losing a little data to waiting. Second, no connection setup: UDP adds no handshake delay, the main reason DNS uses it. Third, no connection state: a server tracks no buffers, sequence numbers or windows per client, so it can serve more clients. Fourth, a small header: 8 bytes, against at least 20 for TCP.

Who uses UDP

Typical users include DNS lookups, network management with SNMP, which must work even when the network is struggling, DHCP address configuration, internet telephony, video conferencing and online games. HTTP/3 runs over QUIC, which runs over UDP, as the topic Evolution of the transport layer: QUIC explains. Freedom has a cost. UDP has no congestion control, so a careless application can flood a link, lose most of its own packets and crowd out TCP flows that politely slow down. Well-behaved UDP applications limit their own rates. Some networks also block or throttle UDP, so many applications fall back to TCP when UDP fails.

Reliability on top of UDP

An application can still have reliable delivery over UDP by building it in: adding its own sequence numbers, acknowledgements and retransmissions, the mechanisms the next topic, Principles of reliable data transfer, develops. This lets the application choose exactly which data deserves a retransmission and how fast to send. QUIC is the leading example: it implements reliability and congestion control above UDP, usually in software outside the operating system's kernel. Doing this well is hard work, which is why most applications that need every byte simply use TCP.

The UDP segment

The header has four fields of 2 bytes each, 8 bytes in all, followed by the application data. Source port: the sender's port, used for replies; it may be 0 if no reply is wanted. Destination port: picks the receiving socket. Length: the size of the whole segment, header plus data, in bytes; it is needed because messages vary in size. The smallest legal value is 8, an empty message, and the largest the 16-bit field allows is 65,535. Checksum: detects corrupted bits. Each UDP segment carries one whole application message, so the receiver gets messages with their boundaries intact, unlike TCP's byte stream.

How the checksum is computed

The sender treats the segment as a series of 16-bit words and adds them using one's complement arithmetic: whenever a sum overflows past 16 bits, the carry is wrapped around and added back into the lowest bit. The checksum is the one's complement of the final sum, found by flipping every bit. The sum also covers a pseudo-header taken from the IP layer: the source and destination IP addresses, the protocol number (17 for UDP) and the UDP length, so a segment delivered to the wrong host is also caught. The receiver adds all the words, including the checksum. With no errors the result is all ones, 1111111111111111. Over IPv4 the checksum is optional, with 0 meaning none; over IPv6 it is mandatory.

Worked example: computing a checksum

Find the checksum of three 16-bit words written in hexadecimal, where each digit stands for 4 bits: 4A3C, B7E1 and 9F05. 1. Add the first two: 4A3C + B7E1 = 1021D. That is 17 bits, so wrap the leading 1 around: 021D + 1 = 021E. 2. Add the third word: 021E + 9F05 = A123, with no overflow. 3. Flip every bit. In hexadecimal, each digit d becomes F minus d, so A123 becomes 5EDC. In binary, the sum 1010000100100011 becomes 0101111011011100. 4. The checksum is 5EDC. Check: A123 + 5EDC = FFFF, all ones, which is what the receiver should get.

Worked example: what the receiver sees

Continue with the words 4A3C, B7E1, 9F05 and checksum 5EDC. 1. With no errors, the receiver's one's complement sum of all four words is FFFF, so the segment is accepted. 2. Suppose noise flips the last bit of the second word, making it B7E0. The sum becomes FFFE; it contains a 0 bit, so the error is detected and the segment is typically discarded. 3. Suppose instead the first word gains 1, becoming 4A3D, and the second loses 1, becoming B7E0. The changes cancel and the sum is FFFF again, so the error slips through. 4. Reordering the words is missed too, because addition does not depend on order. Check: the checksum catches single-bit errors but not every error, and it offers no protection against deliberate tampering.

Why check end to end?

Many links, such as Ethernet, already check frames for errors, so why does UDP bother? Because not every link on the path is guaranteed to check, and a segment can be corrupted while sitting in a router's memory, where no link check sees it. Only the ends can confirm that what arrived is what was sent. This is the end-to-end principle, set out by Saltzer, Reed and Clark in 1984: some functions can be done completely only at the end points, so lower-layer versions are at most performance aids. UDP detects errors but does not recover from them; it discards the damaged segment or, in some implementations, passes it up with a warning.

Common mistakes to avoid

First, saying UDP has no error checking; it has a checksum but does not repair errors. Second, thinking the length field counts only data; it includes the 8-byte header. Third, forgetting to wrap the carry back in when adding words, or forgetting the final bit flip. Fourth, assuming a correct checksum proves the data is intact; compensating errors and reordered words pass. Fifth, treating UDP as always faster; with no congestion control, a flooding sender mostly loses its own packets.

Key takeaways

UDP (RFC 768) adds only ports, a length and a checksum to IP, with no handshake, connection state, reliability or congestion control. Applications choose it for timing control, no setup delay, no per-client state and an 8-byte header: DNS, SNMP, DHCP, real-time media, games and QUIC. The checksum is the one's complement of the one's complement sum of 16-bit words, including a pseudo-header; the receiver expects all ones. It catches many accidental errors but not all, following the end-to-end principle.

Flashcards

Flashcard

UDP

User Datagram Protocol: connectionless, unreliable transport

RFC 768, 1980.

Test your understanding

Question 1 of 5

A UDP segment carries a 120-byte DNS reply. What value is in its length field?

Question 2 of 5

In one's complement addition, 8000 + 9234 (hexadecimal) gives a 17-bit result. What is the 16-bit sum?

Question 3 of 5

The one's complement sum of a segment's words is 6C3A. What is the checksum?

Question 4 of 5

Why do DNS lookups normally use UDP rather than TCP?

Question 5 of 5

A receiver adds all the words of a segment, including the checksum, and gets FFFF. What can it conclude?

← Previous topicNext topic →