4.3.18Computer Networks

UDP — header, use cases, checksum

2,272 words10 min readdifficulty · medium1 backlinks

What UDP actually is

  • Connectionless → no handshake; first packet carries data immediately.
  • Unreliable → no ACKs, no retransmission. Lost = gone.
  • Message-oriented (datagram) → one send() = one datagram = one recv(). (TCP is a byte-stream; UDP preserves message boundaries.)

The UDP Header — derived from "what's the minimum you need?"

HOW do we figure out the header? Don't memorize it — reconstruct it by asking "what must Transport add to IP?"

  1. IP delivers to a machine, but a machine runs many programs. We need to identify which programsource port + destination port (16 bits each, so up to 65535).
  2. The receiver must know where the datagram ends → a length field.
  3. We'd like some error detection (IP only checksums its own header, not the data) → a checksum.

That's it. Four fields, each 16 bits = 8 bytes total.

Figure — UDP — header, use cases, checksum

Compare: UDP header = 8 bytes, TCP header = 20 bytes (minimum). That 12-byte saving + no handshake is the whole point.


The UDP Checksum — derived from scratch

HOW it's built (1's complement sum over 16-bit words):

  1. Treat the data as a sequence of 16-bit integers.
  2. Add them. Whenever the sum overflows 16 bits, wrap the carry around and add it back to the low 16 bits (this is one's-complement addition).
  3. Take the one's complement (flip every bit) of the final sum → that's the checksum you transmit.

WHY one's complement + carry-wrap?

  • Carry-wrap keeps the result inside 16 bits without losing information (the overflow carry isn't discarded, it's recycled).
  • Sending the complement means the receiver can just add everything including the checksum and expect all-ones (0xFFFF0\text{xFFFF}): because x+x=all 1sx + \overline{x} = \text{all 1s}.

Use cases — WHY pick "unreliable"?

Use case Why UDP wins
DNS query One small request, one small reply. TCP handshake (3 packets) would cost more than the lookup.
Video/voice (VoIP, streaming, games) A retransmitted old frame arrives too late to be useful — drop it, keep going.
DHCP, ARP-like bootstrapping Client has no IP/connection yet; broadcast a datagram.
SNMP, NTP Tiny, frequent, loss-tolerant management/time packets.
QUIC / HTTP/3 Builds custom reliability + encryption in userspace on top of UDP to dodge TCP's rigidity.

Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine sending a friend a quick note by sliding it under their door. You don't knock, you don't wait for a "got it!" — you just push it under and leave. If a gust of wind blows it away, you'll never know. That's UDP: super fast, no waiting, but no promise it arrives. To at least catch a torn note, you scribble a tiny number at the bottom equal to the sum of all the words' letter-counts (the checksum); your friend re-adds and if the number's wrong, they know the note got smudged and just throw it out. Great for sending "the game ball is at position X" 60 times a second — if one note is lost, the next one fixes it anyway.


Flashcards

How many bytes is the UDP header and how many fields?
8 bytes, 4 fields (each 16 bits): Source Port, Destination Port, Length, Checksum.
What does the UDP Length field count?
Header + data = 8 + payload bytes; minimum value is 8 (empty payload).
Why is UDP called "unreliable" if it has a checksum?
It detects corruption (checksum) but does not recover — no ACKs or retransmission. Corrupt datagrams are silently dropped.
How is the UDP checksum computed?
One's-complement sum of all 16-bit words (with carry wrap-around), then take the one's complement of the result.
What should the receiver get when summing all words plus the checksum?
All ones, 0xFFFF (in one's-complement arithmetic) → datagram accepted.
Why is the carry wrapped around in one's-complement addition?
To keep the result within 16 bits without discarding the overflow information.
What does a checksum field of 0x0000 mean in UDP (IPv4)?
Checksum disabled/not computed. A genuine zero result is sent as 0xFFFF instead.
What is the pseudo-header and why include it in the checksum?
Src IP, Dst IP, protocol=17, UDP length. Folding the dest IP in lets the receiver detect datagrams misdelivered to the wrong machine.
TCP vs UDP: stream or message?
TCP = byte stream (no boundaries). UDP = message/datagram-oriented (one send = one recv, boundaries preserved).
Give three classic UDP use cases and the reason.
DNS (tiny request/reply, handshake too costly), VoIP/video/games (late retransmit is useless), DHCP/SNMP/NTP (small loss-tolerant control packets).
Why does HTTP/3 (QUIC) run over UDP?
To build custom reliability + encryption in userspace, avoiding TCP's rigidity and head-of-line blocking.
Can a correct checksum still pass corrupted data?
Yes — two compensating errors (e.g. +1 and −1 in different words) cancel; 16-bit checksum is weak detection, not cryptographic integrity.
TCP header min size vs UDP header size?
TCP = 20 bytes minimum; UDP = 8 bytes.

Connections

  • TCP — header, three-way handshake, reliability (the reliable counterpart)
  • IP — datagrams and addressing (UDP is a thin layer on top)
  • DNS resolution (primary UDP user)
  • QUIC and HTTP-3 (reliability rebuilt over UDP)
  • One's complement arithmetic (the checksum math)
  • Transport layer — ports and multiplexing (why port fields exist)

Concept Map

thin wrapper over

is

is

is

adds

adds

identify program

marks datagram end

detects corruption

no handshake

no ACK or retry

only 8 bytes

uses

UDP Datagram Protocol

IP Layer

Connectionless

Unreliable

Message-oriented

Port Numbers

Length Field

Checksum

8-byte Header

Fast Low Overhead

Ones-complement sum

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, UDP ko samajhne ka sabse easy tareeka: TCP ek registered courier hai jo delivery confirm karta hai, lost packet dobara bhejta hai, order maintain karta hai. UDP us se ulta — ek postcard letterbox mein daal ke chal diye. Na handshake, na "mila kya?" ka jawab, na retry. Bas fast aur halka. Isliye iska header sirf 8 bytes ka hai: Source Port, Destination Port, Length, aur Checksum — sirf 4 fields. Compare karo TCP ke 20-byte header se, samajh aa jayega ki UDP "minimum-overhead" pe chalta hai.

Ab "unreliable" ka matlab galat mat samajhna. Unreliable ka matlab recovery nahi — yaani lost packet wapas nahi aayega. Lekin error detect zaroor hota hai checksum se. Checksum kaise banta hai? Saare 16-bit words ko jodo, jab 16 bits se overflow ho to carry ko wapas neeche add karo (one's complement addition), phir poore sum ka complement (ulta) le lo — wahi checksum transmit hota hai. Receiver sab kuch jod ke dekhta hai, agar 0xFFFF (saare ones) aaya to data theek, warna datagram chup-chaap discard. App ko khabar bhi nahi lagti.

Use cases yaad rakhne ke liye soch: jahan time zaroori hai, completeness se zyada. DNS query — chhoti si request, chhota reply, handshake ki zaroorat hi nahi. Video call, online game, VoIP — purana frame late aaye to bekaar, isliye drop karo aur aage badho. DHCP, NTP, SNMP — chhote control packets. Aur modern HTTP/3 (QUIC) bhi UDP ke upar apni reliability khud banata hai, TCP ki rigidity se bachne ke liye.

Ek important point exam ke liye: checksum mein pseudo-header bhi shaamil hota hai (source IP, dest IP, protocol 17, length). Isse galat machine pe deliver hua datagram bhi pakda jaata hai. Aur agar checksum field 0x0000 ho to matlab "checksum off" (IPv4 mein optional) — agar asli calculation se 0 aaye to 0xFFFF bhejte hain. IPv6 mein checksum compulsory hai. Bas itna pakka kar lo, UDP clear ho jayega.

Go deeper — visual, from zero

Test yourself — Computer Networks

Connections