# UDP Header
## Structure of the UDP Header
The UDP header is remarkably simple compared to the TCP header. It is only 8 bytes long and contains just four fields. This simplicity reflects UDP's design philosophy of providing minimal transport layer services with low overhead. The small header size contributes to UDP's efficiency and speed advantage over TCP.
## UDP Header Fields
The source port field is 16 bits wide and optionally identifies the application process on the sending host. When a client application sends a UDP datagram, the operating system assigns an ephemeral source port number. The source port allows the receiver to send a reply to the correct application on the sender. If the sender does not need a reply, the source port may be set to zero.
The destination port field is 16 bits wide and identifies the application process on the receiving host. The destination port is used by the receiving host to direct the UDP datagram to the correct application. DNS uses destination port 53, DHCP uses port 67 for server and 68 for client, SNMP uses port 161, and NTP uses port 123.
The length field is 16 bits wide and specifies the total length of the UDP datagram including both the header and the data, measured in bytes. The minimum value is 8, representing an empty datagram with only the header and no data. The maximum value is 65,535 bytes, though the actual maximum data size is limited by the underlying IP layer's maximum transmission unit.
The checksum field is 16 bits wide and provides error detection. The checksum is calculated over the UDP header, the UDP data, and a pseudo-header containing the source IP address, destination IP address, protocol number, and UDP length. In IPv4, the checksum is optional. If a sender chooses not to calculate a checksum, it sets the checksum field to all zeros. If a non-zero value is in the checksum field, the receiver verifies it. In IPv6, the checksum is mandatory because IPv6 does not include a header checksum.
## Comparison with TCP Header
The 8-byte UDP header is dramatically smaller than the 20-byte minimum TCP header. UDP lacks sequence numbers, acknowledgment numbers, flags for connection control, and a window size field, because UDP does not implement the features these fields support. This compactness makes UDP suitable for applications that handle their own application-level reliability or that simply do not need reliability guarantees.Back to Subject