Your free access ends in 7 days β€” and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
← All chapters
Chapter 14Β· 6 min read Β· illustrated

Anatomy of a UDP Datagram

The whole UDP header is just four fields and eight bytes

UDP’s simplicity shows in its header β€” it has only four fields, totalling 8 bytes. This short chapter labels each one so you know exactly what UDP adds to your data.

01

The datagram = header + data

A UDP datagram is two parts: an 8-byte header followed by your data (the payload). Everything UDP does is captured in those 8 header bytes β€” four fields of 2 bytes each.

Tap to enlarge
02

Field 1 β€” Source Port

Source Port (2 bytes) identifies the sending application, so replies can find their way back to the right process. It is optional in UDP and may be 0 if no reply is expected.

Tap to enlarge
03

Field 2 β€” Destination Port

Destination Port (2 bytes) says which application on the receiving machine should get the datagram β€” for example, port 53 for DNS. This is how one IP can run many services at once.

Tap to enlarge
04

Field 3 β€” Length

Length (2 bytes) is the size of the entire datagram β€” header plus data β€” in bytes. The minimum is 8 (a header with no data).

Tap to enlarge
05

Field 4 β€” Checksum

Checksum (2 bytes) is an error-detection code covering the header, data, and a small pseudo-header of IP info. It lets the receiver detect corruption in transit. It is optional in IPv4 (0 = unused) but mandatory in IPv6.

Tap to enlarge
06

Eight bytes, that’s it

Add it up: Source Port + Destination Port + Length + Checksum, each 2 bytes, equals an 8-byte header. That tiny, fixed header is exactly why UDP is so lightweight and fast.

Tap to enlarge