User Datagram Protocol (UDP)

In this tutorial, you will learn the basic concepts of User Datagram Protocol (UDP). After reading this tutorial, you will know the ports, header, service, operations, uses and queues of UDP.

Contents:

  1. User Datagram Protocol (UDP)
  2. Ports of UDP
  3. UDP Header
  4. Connectionless Service
  5. Basic Operations of UDP
  6. Queues in UDP
  7. Remote Procedure Call (RPC)
  8. Uses of UDP

User Datagram Protocol (UDP)

User Datagram Protocol (UDP) is one of the protocols of the transport layer. It provides a connectionless service, or you can say it is a connectionless protocol. Basically, it just sends packets from sender to receiver, which means that the path between sender and receiver is not established. UDP does not guarantee packet delivery.

  • The transport layer receives data from the application and creates segments by adding UDP headers to the data. The UDP protocol transmits segments from sender to receiver before establishing a connection.
  • In general, UDP is preferable if data has to be sent quickly, regardless of packet loss.
  • Because UDP is a connectionless protocol, it is also known as a stateless protocol, which means that it does not track the state of the communication session.
  • UDP communicates with the application layer and obtains reliability for communication.
  • Applications that tolerate less data loss can use the UDP protocol. For example, audio and video conferencing tolerate some data loss, but communication is faster.

Ports of UDP

UDP has port numbers that are used when the messages are transmitted from sender to receiver. Generally, when UDP is used for communication, there is less interaction between sender and receiver, which increases the transmit data rate.

  • Here, a datagram means a block of communication. In UDP, a block of communication is known as a datagram or segment.

The diagram below describes the well-known ports used with UDP.

advertisement
advertisement
well-known ports used with UDP

As shown in the figure, different types of ports are used for different purposes. For example, echo packets are used with port number 7 when the receiver wants to send an acknowledgment to the sender.

UDP Header

UDP Header has only four fields so it is simpler than the TCP header. The fixed-size of UDP header is 8 bytes.

The below diagram shows the fields of UDP header.

fields of UDP header
  • As shown in the figure, the source and destination port numbers, total length, and checksum are the fields of the UDP header.
  • Source Port Number: The size of the source port number is 16 bits, which means that it has a range of 0 to 65535. It runs on the process of the source machine. If the source is a client and wants a request to the server, it uses the ephemeral port number, and the destination port number is set to the well-known port number.
  • Destination Port Number: The size of the destination port number is 16 bits. If the destination is the server, the port number is the well-known port address, and if it is the client, the port number is ephemeral.
  • Total Length: The Total Length field is used to define the length of the user datagram or packet, headers, and data. Normally, a UDP datagram is encapsulated in the IP header at the network layer, so in most cases, it is not necessary to have a length field in a UDP datagram because IP packets have a length field that defines the total length.
  • Checksum: The checksum is an error detection method used to detect errors during communication across the entire UDP datagram.

Connectionless Service

When the transport layer generates a UDP datagram, each UDP datagram is an independent datagram. There is no relation between UDP datagrams, even if they are coming from the same source machine’s process.

  • As soon as the transport layer completes the creation of UDP datagrams, all datagrams are independently sent to the network.
  • There is no sequence number in a UDP datagram. All datagrams will be transmitted through separate paths as there is no connection establishment and connection termination mechanism.
  • The receiver does not receive the data in sequence because there is no sequence number in the datagram. Therefore, it may not segment the data properly, and there is a chance that some datagrams may be lost during transmission. In simple words, there is no in-order delivery in the UDP protocol.

The figure below explains the transmission of UDP datagrams.

transmission of UDP datagrams
  • As shown in the figure, the transport layer of the source machine generates five UDP datagrams from the same process and sends them to Router-1.
  • When Router-1 receives UDP datagrams, it sends them in different paths because the connection has not been established beforehand.
  • A packet loss occurs during transmission. The receiver only receives four UDP datagrams out of five datagrams.
  • Here, connectionless service is used in the UDP protocol, so the UDP protocol does not make any guarantees to deliver the datagrams.

Basic Operations of UDP

We will discuss flow and error control, encapsulation, and decapsulation in this section.

advertisement

Flow and Error Control: Since UDP uses a connectionless service, it is an unreliable protocol of the transport layer. It does not have a flow control mechanism and therefore does not use any algorithms to control overflow. Due to this problem, the receiver may overflow with incoming packets and result in network congestion.

  • Furthermore, UDP has no error control mechanism except for checksums. If the packet is duplicated or lost during transmission, the sender does not know this. The receiver silently discards the packet when an error is detected in the packet through the checksum.

Encapsulation and Decapsulation: On the sending machine, the transport layer receives the data from the application layer and adds a UDP header, this process is known as encapsulation. At the receiver, the reverse process is performed. The receiver receives the packet from the network layer and removes the IP header and then the UDP header, a process known as decapsulation.

The figure below explains the encapsulation and decapsulation of UDP headers.

encapsulation and decapsulation of UDP headers
  • As shown in the figure, the transport layer on the sender side encapsulates the data by adding UDP headers. It sends the UDP datagram to the receiver machine.
  • The receiver machine decapsulates the IP packet by removing the IP header and UDP header and sends the data to the process running on the receiver’s machine.

Queues in UDP

Incoming and outgoing queues are created in client and server machines when client and server mechanisms are used. For each incoming packet, the machine checks that an incoming queue has been created for the port number of the incoming packet. Similarly, the machine also checks the outgoing queue.

advertisement

The figure below explains the queues in UDP.

Incoming Queue and Outgoing Queue in UDP
  • As shown in the figure, the client wants to know the date and time of the server.
  • The client machine initiates the process of obtaining the date and time of the server by asking for the port number from the operating system.
  • During this process, incoming and outgoing queues are created and attached to each process.
  • The client machine chooses a source port number from the ephemeral port numbers, which is 52000. The well-known port number for date and time service is 13.
  • Therefore, the client machine sets the destination port address as 13 and sends the message to the outgoing queue.
  • The outgoing queue adds a UDP header and delivers it to the network layer.The client’s request is received by the server.
  • On the server-side, queues are created using well-known ports. If the incoming queue for incoming packets comes from the client, the server further processes it. Otherwise, it sends a port unreachable message to the client.
  • If everything is working properly, the server sets the source port address as its well-known port address and the destination port address as the client’s port address and sends the date-time information to the client.

Remote Procedure Call (RPC)

When the client requests services from the server, it sends some required parameters to the server, and the server replies to the client. Here, the request-reply interaction over the network is known as the procedure call. This type of mechanism helps the applications to run smoothly over the network.

  • For example, the user types the URL of the website, and the machine communicates with the DNS server to obtain the IP address of the website by sending a UDP packet.
  • RPC allows processes running on machines to call processes located on remote hosts.
  • When RPC is used, the client machine process calls a procedure on the server by sending a UDP packet. Then the client process is suspended, and the server process executes. The server interprets the UDP packet and sends the process result to the client. Here, the message exchange from the application programmer is hidden. This is known as the Remote Procedure Call (RPC).
  • The calling procedure and called procedure are used that belong to client and server, respectively.
  • The client tied to the procedure is known as the client stub, and the server tied to the procedure is known as the server stub.

The below diagram explains Remote Procedure Call.

Remote Procedure Call
  • First, the client calls the client stub, as shown in the figure. The client stub binds the parameters to the message, which is known as marshaling. The client stub makes a system call and sends a message to the operating system.
  • On receiving the message, the client’s OS sends the message to the server, and the server sends the incoming message to the server stub.
  • The server stub opens the message and calls the server procedure. Then the server replies to the client’s message as a response.

Uses of UDP

Below is a list of the uses of the UDP protocol.

  • If flow and error control are not required, and the process requires simple request-response communication, the UDP protocol is suitable for communication. For example, DHCP and DNS in which the end device sends the request but it may not get a reply back.
  • Generally, UDP does not have flow and error control but is used for processes that have their own flow and error control mechanisms. For example, TFTP (Trivial File Transfer Protocol) and SNMP (Simple Network Management Protocol) have their own flow and error control mechanisms, so UDP is used for faster transmission.
  • UDP is good for live video and multimedia applications as these applications can tolerate some data loss but require fast communication. For example, video conferencing, live streaming of video, and Voice over IP (VoIP).
  • UDP is also used in RIP (Routing Information Protocol), one of the route updating protocols.

Key Points to Remember

Here is the list of key points we need to remember about “User Datagram Protocol”.

  • UDP sends packets from sender to receiver and the path between sender and receiver is not established. UDP does not guarantee packet delivery.
  • Generally, when UDP is used for communication, there is less interaction between sender and receiver, which increases the transmit data rate.
  • UDP Header has only four fields so it is simpler than the TCP header. The fixed-size of UDP header is 8 bytes. In UDP, a block of communication is known as a datagram or segment.
  • The source and destination port numbers, total length, and checksum are the fields of the UDP header.
  • UDP does not have a flow control mechanism and therefore does not use any algorithms to control overflow.
  • Incoming and outgoing queues are created in client and server machines when client and server mechanisms are used.
  • When RPC is used, the client machine process calls a procedure on the server by sending a UDP packet. Then the client process is suspended, and the server process executes.
  • DHCP, DNS, TFTP, SNMP, VoIP, and RIP can use UDP service for communication.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.