Communication Networks/TCP and UDP Protocols/UDP

From Wikibooks, open books for an open world
Jump to: navigation, search


Unlike TCP, UDP doesn't establish a connection before sending data, it just sends. Because of this, UDP is called "Connectionless". UDP packets are often called "Datagrams". An example of UDP in action is the DNS service. DNS servers send and receive DNS requests using UDP.


In this section we have to look at User Datagram protocol. It’s a transport layer protocol. This section will cover the UDP protocol, its header structure & the way with which it establishes the network connection.

As shown in Figure 1,the User Datagram Protocol (UDP) is a transport layer protocol that supports Network Application. It layered on just below the ‘Session’ and sits above the IP(Internet Protocol) in open system interconnection model (OSI). This protocol is similar to TCP (transmission control protocol) that is used in client/ server programs like video conference systems expect UDP is connection less.

Fig1 osilayer.jpg

Figure 1:UDP in OSI Layer Model

What is UDP?[edit]

Fig2 UDPwork.jpg

'Figure 2:UDP

UDP is a connectionless and unreliable transport protocol.The two ports serve to identify the end points within the source and destination machines. User Datagram Protocol is used, in place of TCP, when a reliable delivery is not required.However, UDP is never used to send important data such as web-pages, database information, etc. Streaming media such as video,audio and others use UDP because it offers speed.

Why UDP is faster than TCP?

The reason UDP is faster than TCP is because there is no form of flow control. No error checking,error correction, or acknowledgment is done by UDP.UDP is only concerned with speed. So when, the data sent over the Internet is affected by collisions, and errors will be present.

UDP packet's called as user datagrams with 8 bytes header. A format of user datagrams is shown in figur 3. In the user datagrams first 8 bytes contains header information and the remaining bytes contains data.

Fig3 udp userdatagrams.jpg

Figure 3:UDP datagrams

Source port number: This is a port number used by source host,who is transferring data. It is 16 bit longs. So port numbers range between 0 to 65,535.

Destination port number: This is a port number used by Destination host, who is getting data. It is also 16 bits long and also same number of port range like source host.

length: Length field is a 16 bits field. It contains the total length of the user datagram, header and data.

Checksum: The UDP checksum is optional. It is used to detect error fro the data. If the field is zero then checksum is not calculated. And true calculated then field contains 1.

Characteristics of UDP

The characteristics of UDP are given below.

• End-to-end. UDP can identify a specific process running on a computer.

• Unreliable, connectionless delivery (e.g. USPS)::

UDP uses a connectionless communication setup. In this UDP does not need to establish a connection before sending data. Communication consists only of the data segments themselves

• Same best effort semantics as IP

• No ack, no sequence, no flow control

• Subject to loss, duplication, delay, out-of-order, or loss of connection

• Fast, low overhead

1.Suit for reliable, local network

2.RTP(Real-Time Transport Protocol)

Use of ports in Communication[edit]

After receiving the data,computer must have some mechanism what to do with it.Consider that user has three application open say a web browser,a telnet session and FTP session.All three application are moving data over the network. So, there should be some mechanism for determining what piece of traffic is bound for which application by operating system.To handle this situation , network ports are used.Available port's range is 0 to 65535.In them 0 to 1023 are well-known ports, 1023 to 49151 are registered ports and 49152 to 65535 are dynamic ports.


Figure 4: Port

List of well-known ports used by UDP:

Fig5 udpport.jpg

Figure 5:List of ports used by UDP

UDP Header structure[edit]

It contains four section. Source port, Destination port, Length and Checksum.

Header of UDP.jpg

Figure 6: UDP Header

Source port

Source port is an optional field. When used, it indicates the port of the sending process and may be assumed to be the port to which a reply should be addressed in the absence of any other information. If not used, a value of zero is inserted.

Destination port

It is the port number on which the data is being sent.


It include the length of UDP Header and Data.

The length in octets of this user datagram, including this header and the data. The minimum value of the length is eight.


The main purpose of checksum is error detection.It guarantees that message arrived at correct destination.To verify checksum,the receiver must extract this fields from IP Header .12-byte psuedo header is used to compute checksum.


It is the application data.or Actual message.

Ethereal Capture

The UDP packet can be viewed using Ethereal capture. One such UDP packet is captured and shown below.

Header of ethereal.jpg

Figure 7: ethereal capture

Communication in UDP[edit]

In UDP connection,Client set unique source port number based on the program they started connection. UDP is not limited to 1-to-1 interaction. A 1-to-many interaction can be provided using broadcast or multi-cast addressing . A many-to-1 interaction can be provided by many clients communicating with a single server. A many-to-many interaction is just an extension of these techniques.

UDP Checksum and Pseudo-Header[edit]

The main purpose of UDP checksum is to detect errors in transmitted segment.

UDP Checksum is optional but it should always be turned on.

To calculate UDP checksum a "pseudo header" is added to the UDP header. The field in the pseudo header are all taken from IP Header. They are used on receiving system to make sure that IP datagram is being received by proper computer. Generally , the pseudo-header includes:

Psuedo Header1.jpg

Figure 8 : UDP Psuedo Header

IP Source Address 4 bytes

IP Destination Address 4 bytes

Protocol 2 bytes

UDP Length 2 bytes

Checksum Calculation[edit]

Sender side :

1. It treats segment contents as sequence of 16-bit integers.

2. All segments are added. Let's call it sum.

3. Checksum : 1's complement of sum.(In 1's complement all 0s are converted into 1s and all 1s are converted into 0s).

4. Sender puts this checksum value in UDP checksum field.

Receiver side :

1. Calculate checksum

2. All segments are added and than sum is added with sender's checksum.

3. Check that any 0 bit is presented in checksum. If receiver side checksum contains any 0 than, error is detected. So,the packet is discarded by receiver.

Here we have explained simple checksum calculation. As an example, suppose that we have the bitstream 0110011001100110 0110011001100110 0000111100001111:

This bit stream is divided into segments of 16-bits integers.

So, it looks like this:

0110011001100110 (16 bits integer segment)



The sum of first of these 16-bits words is:




Adding the third word to the above sum gives



1100101011001010 (sum of all segments)

Now to calculate checksum 1's complement of sum is taken. As I mentioned earlier , 1's complement is achieved by converting all 1s into 0s and all 0s into 1s. So,the checksum at sender side is : 0011010100110101.

Now at the receiver side, again all segments are added . and sum is added with sender's checksum.

If no error than check of receiver would be : 1111111111111111.

If any 0 bit is presented in the header than there is an error in checksum.So,the packet is discarded.

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 -- one of the links may use a protocol that does not provide error checking. 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 simply discard the damaged segment; others pass the damaged segment to the application with a warning.


UDP is a transport layer protocol. UDP is a connectionless and unreliable protocol. UDP does not do flow control, error control or retransmission of a bad segment. UDP is faster then TCP. UDP is commonly used for streaming audio and video . UDP never used for important documents like web-page, database information, etc. UDP transmits segments consisting of an 8-byte header. Its contains Source port, Destination port, UDP length and Checksum. UDP checksum used for detect “errors” in transmitted segment.

Exercise Questions[edit]

1. Calculate UDP checksum of the following sequence: 11100110011001101101010101010101.

Answer : To calculate checksum follow the following steps:

       1. first of all divide the bit stream on to two parts of 16-bit each.
          So,the two bit stream will be  1110011001100110 and  1101010101010101.
       2. Add this two bit stream.So ,the addition will be 
              1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
              1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
            1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1  
              1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 0   
       3. Now apply one's complement to this bit's complement is achieved by converting all 1s into 0s and all 0s into 1s.
          So, the checksum will be : 0100010001000011.

2. What is the advantage of keeping checksum field turned off and when it is appropriate to keep checksum field turned off?

Answer  :

           By keeping checksum field turned off,this might save computational load and speed up data transfer.
           When we are transmitting data over wide area network(WAN) , it is not a good idea to keep checksum off.
           We can keep checksum turned off when we are transmitting data over a Local Area Network(LAN),because switching infrastructure   
           would catch transmission error in the Ethernet protocol's checksum