The data transmitted through UDP is organized into units called datagrams or frames.
The UDP frame format consists of four primary fields, each of which is 2 bytes (16 bits) long. These fields are as follows:
Source Port (16 bits): This field specifies the source port number, which identifies the sending application on the source device. It helps the recipient device know which application to pass the received data to. In some cases, this field may be optional, and the value can be set to zero if not used.
Destination Port (16 bits): This field specifies the destination port number, which identifies the target application on the receiving device. It is essential for proper communication between devices, as it tells the recipient which application the incoming data is intended for.
Length (16 bits): This field specifies the length of the entire UDP datagram, including the header and data payload. The minimum value for this field is 8 bytes, which is the size of the header alone. The maximum value is 65,535 bytes, which is the largest possible size of an IP datagram.
Checksum (16 bits): This field is used for error checking and is generated by the sender. It is calculated based on the contents of the UDP header, data payload, and a pseudo-header that includes some fields from the IP header. The receiver calculates the checksum upon receiving the datagram and compares it to the value in this field. If they don't match, the datagram is considered corrupted, and the receiver typically discards it. If the sender does not want to use the checksum, this field can be set to zero.