UDP receives the data and adds the UDP header. UDP then passes the user datagram to the IP with the socket addresses. IP adds its own header. The IP datagram is then passed to the data link layer. The data link layer receives the IP datagram, adds its own header and a trailer (possibly), and passes it to the physical layer.
The physical layer encodes bits into electrical or optical signals and sends it to the remote machine. The IP datagram contains its total length in bytes, so the length of the UDP datagram is this total length minus the length of the IP header. The UDP header is shown by the fields:
- Source port numbers (16 bits): This 16-bit port number identifies the sending process running on the source host. Since the source port number is 16 bits long, it can range from 0 to 65 656 bytes.
If the source host is the client, the client program is assigned a random port number called the ephemeral port number requested by the process and chosen by the UDP software running on the source host. If the source host is the server, the port number is a universal port number.
- Destination port numbers (16 bits): This is the 16-bit port number used by the process running on the destination host. If the destination host is the server, the port number is a universal port number, while if the destination host is the client, the port number is an ephemeral port number.
- Length (16 bits): This is a 16-bit field that contains a count of bytes in the UDP datagram, including the UDP header and the user data. This 16-bit field can define a total length of 0 to 65 535 bytes. However, the minimum value for length is eight, which indicates an UDP datagram with only header and no data.
Therefore, the length of data can be between 0 to 65 507 bytes, subtracting the total length 65 535 bytes from 20 bytes for an IP header and 8 bytes for an UDP header. The length field in a UDP user datagram is redundant. The IP datagram contains its total length in bytes, so the length of the UDP datagram is this total length minus the length of the IP header.
- Checksum (16 bits): The UDP checksum is used to detect errors over the entire user datagram covering the UDP header and the UDP data. UDP checksum calculations include a pseudoheader, the UDP header and the data coming from the application layer.
The value of the protocol field for UDP is 17. If this value changes during transmission, the checksum calculation at the receiver will detect it and UDP drops the packet.
the packet. The checksum computation at the sender is as follows:
- Add the pseudoheader to the UDP datagram.
- Fill the checksum field with zero.
- Divide the total bits into 16-bit words.
- If the total number of bytes is not even, add padding of all 0s.
- Complement the 16-bit result and insert it in the checksum field.
- Drop the pseudoheader and any added padding.
- Deliver the UDP datagram to the IP software for encapsulation.
The checksum computation at the receiver is as follows:
- Add the pseudoheader to the UDP datagram.
- Add padding if needed.
- Divide the total bits into 16-bit words.
- Add all 16-bit sections using arithmetic.
- Complement the result.
- If the result is all 0s, drop the pseudoheader and any added padding and accept the user datagram. Otherwise, discard the user datagram.
Multiplexing and Demultiplexing
In a host running a TCP/IP suite, there is only one UDP but there may be several processes that may want to use the services of UDP. To handle this situation, UDP needs multiplexing and demultiplexing.
- Multiplexing: At the sender side, it may have several processes that need user datagrams. But there is only one UDP. This is a many-to-one relationship and requires multiplexing. UDP accepts messages from different processes, differentiated by their assigned port numbers. After adding the header, UDP passes the user datagram to IP.
- Demultiplexing: At the receiver side, there is only one UDP. However, it may happen to be many processes that can receive user datagrams. This is a one-to-many relationship and requires demultiplexing. UDP receives user datagrams from IP. After error checking and dropping of header, UDP delivers each message to the appropriate process based on the port numbers.
UDP is suitable for a process that requires simple request-response communication with little concern for flow and error control. It is not suitable for a process that needs to send bulk data, like FTP. However, UDP can be used for a process with internal flow and error control mechanisms such as the Trivial File Transfer Protocol (TFTP) process. UDP is also used for management processes such as SNMP.