UDP Segment Structure

UDP Segment Structure

The application data shown in Figure 1 occupies the data field of the UDP segment. For instance, for DNS, the data field includes either a query message or a response message. For a streaming audio application, audio samples fill the data field. The UDP header has only four fields, each consisting of two bytes. As explained in the previous section, the port numbers allow the destination host to pass the application data to the correct process running on the destination end system (that is, to perform the demultiplexing function). The checksum is used by the receiving host to check whether errors have been introduced into the segment. In fact, the checksum is also calculated over a few of the fields in the IP header in addition to the UDP segment. But we ignore this detail in order to see the forest through the trees. We'll discuss the checksum calculation below. Basic principles of error detection are described in "Error Detection and Correction Techniques". The length field specifies the length of the UDP segment, including the header, in bytes.

UDP Checksum

The UDP checksum provides for error detection. That is, the checksum is used to find out whether bits within the UDP segment have been altered (for instance, by noise in the links or while stored in a router) as it moved from source to destination. UDP at the sender side performs the 1s complement of the sum of all the 16-bit words in the segment, with any overflow encountered during the sum being wrapped around. This result is put in the checksum field of the UDP segment. Here we give a simple example of the checksum calculation.
UDP segment structure

As an example, assume that we have the following three 16-bit words:

The sum of first two of these 16-bit words is

Adding the third word to the above sum gives

Note that this last addition had overflow, which was wrapped around. The 1s complement is achieved by converting all the 0s to 1s and converting all the 1s to 0s. Therefore the 1s complement of the sum 0100101011000010 is 1011010100111101, which becomes the checksum. At the receiver, all four 16-bit words are added, including the checksum. If no errors are introduced into the packet, then clearly the sum at the receiver will be 1111111111111111. If one of the bits is a 0, then we know that errors have been introduced into the packet.

You may wonder why UDP provides a checksum in the first place, as many link-layer protocols (including the popular Ethernet protocol) also provide error checking. The reason is that there is no guarantee that all the links between source and destination provide error checking; that is, one of the links may use a link-layer protocol that does not provide error checking. Moreover, even if segments are correctly transferred across a link, it's possible that bit errors could be introduced when a segment is stored in a router's memory. Given that neither link-by-link reliability nor in-memory error detection is guaranteed, UDP must provide error detection at the transport layer, on an end-end basis, if the end-end data transfer service is to provide error detection. This is an instance of the celebrated end-end principle in system design [Saltzer 1984], which states that since certain functionality (error detection, in this case) must be implemented on an end-end basis:  "functions placed at the lower levels may be redundant or of little value when compared to the cost of providing them at the higher level".

Because IP is supposed to run over just about any layer-2 protocol, it is useful for the transport layer to provide error checking as a safety measure. Although UDP provides error checking, it does not do anything to recover from an error. Some implementations of UDP just remove the damaged segment; others pass the damaged segment to the application with a warning.

That wraps up our discussion of UDP. We will soon see that TCP offers reliable data transfer to its applications as well as other services that UDP doesn't offer. Naturally, TCP is also more complicated than UDP. Before discussing TCP, however, it will be useful to step back and first discuss the underlying principles of reliable data transfer.


application data, query message, response message, end system, checksum, udp segment

Copy Right

The contents available on this website are copyrighted by TechPlus unless otherwise indicated. All rights are reserved by TechPlus, and content may not be reproduced, published, or transferred in any form or by any means, except with the prior written permission of TechPlus.