UDP Protocol
UDP Protocol
In computer networking, the UDP stands for User Datagram Protocol. The David P. Reed
developed the UDP protocol in 1980. It is defined in RFC 768, and it is a part of
the TCP/IP
protocol, so it is a standard protocol over the internet. The UDP protocol allows the computer
applications to send the messages in the form of datagrams from one machine to another
machine over the Internet Protocol (IP)
network. The UDP is an alternative communication protocol to the TCP protocol (transmission
control protocol). Like TCP, UDP provides a set of rules that governs how the data should be
exchanged over the internet. The UDP works by encapsulating the data into the packet and
providing its own header information to the packet. Then, this UDP packet is encapsulated to the
IP packet and sent off to its destination. Both the TCP and UDP
protocols send the data over the internet protocol network, so it is also known as TCP/IP
and UDP/IP. There are many differences between these two protocols. UDP enables the process
to process communication, whereas the TCP provides host to host communication. Since UDP
sends the messages in the form of datagrams, it is considered the best-effort mode of
communication. TCP
sends the individual packets, so it is a reliable transport medium. Another difference is that the
TCP is a connection-oriented protocol whereas, the UDP is a connectionless protocol as it does
not require any virtual circuit to transfer the data.
UDP also provides a different port number to distinguish different user requests and
also provides the checksum capability to verify whether the complete data has arrived or
not; the IP
UDP
In the case of UDP, the datagrams are sent in some order will be received in the same
order is not guaranteed as the datagrams are not numbered.
o Ports
The UDP protocol uses different port numbers so that the data can be sent to the
correct destination. The port numbers are defined between 0 and 1023.
o Faster transmission
o Acknowledgment mechanism
The UDP does have any acknowledgment mechanism, i.e., there is no handshaking
between the UDP sender and UDP receiver. If the message is sent in TCP, then the
receiver acknowledges that I am ready, then the sender sends the data. In the case of
TCP, the handshaking occurs between the sender and the receiver, whereas in UDP,
there is no handshaking between the sender and the receiver.
Each UDP segment is handled individually of others as each segment takes different
path to reach the destination. The UDP segments can be lost or delivered out of order
to reach the destination as there is no connection setup between the sender and the
receiver.
o Stateless
It is a stateless protocol that means that the sender does not get the acknowledgement
for the packet which has been sent.
Why do we require the UDP protocol?
As we know that the UDP is an unreliable protocol, but we still require a UDP protocol in
some cases. The UDP is deployed where the packets require a large amount of
bandwidth along with the actual data. For example, in video streaming, acknowledging
thousands of packets is troublesome and wastes a lot of bandwidth. In the case of video
streaming, the loss of some packets couldn't create a problem, and it can also be
ignored.
In UDP, the header size is 8 bytes, and the packet size is upto 65,535 bytes. But this
packet size is not possible as the data needs to be encapsulated in the IP datagram, and
an IP packet, the header size can be 20 bytes; therefore, the maximum of UDP would be
65,535 minus 20. The size of the data that the UDP packet can carry would be 65,535
minus 28 as 8 bytes for the header of the UDP packet and 20 bytes for IP header.
o Source port number: It is 16-bit information that identifies which port is going t
send the packet.
o Destination port number: It identifies which port is going to accept the
information. It is 16-bit information which is used to identify application-level
service on the destination machine.
o Length: It is 16-bit field that specifies the entire length of the UDP packet that
includes the header also. The minimum value would be 8-byte as the size of the
header is 8 bytes.
o Checksum: It is a 16-bits field, and it is an optional field. This checksum field
checks whether the information is accurate or not as there is the possibility that
the information can be corrupted while transmission. It is an optional field, which
means that it depends upon the application, whether it wants to write the
checksum or not. If it does not want to write the checksum, then all the 16 bits
are zero; otherwise, it writes the checksum. In UDP, the checksum field is applied
to the entire packet, i.e., header as well as data part whereas, in IP, the checksum
field is applied to only the header field.
In UDP protocol, numbers are used to distinguish the different processes on a server
and client. We know that UDP provides a process to process communication. The client
generates the processes that need services while the server generates the processes that
provide services. The queues are available for both the processes, i.e., two queues for
each process. The first queue is the incoming queue that receives the messages, and the
second one is the outgoing queue that sends the messages. The queue functions when
the process is running. If the process is terminated then the queue will also get
destroyed.
UDP handles the sending and receiving of the UDP packets with the help of the
following components:
o Input queue: The UDP packets uses a set of queues for each process.
o Input module: This module takes the user datagram from the IP, and then it
finds the information from the control block table of the same port. If it finds the
entry in the control block table with the same port as the user datagram, it
enqueues the data.
o Control Block Module: It manages the control block table.
o Control Block Table: The control block table contains the entry of open ports.
o Output module: The output module creates and sends the user datagram.
Several processes want to use the services of UDP. The UDP multiplexes and
demultiplexes the processes so that the multiple processes can run on a single host.
Limitations
Advantages
Before you can send or receive UDP packets, whether you are the client or the server, you need to
create a socket to talk to using the socket() function, and pass
the SOCK_DGRAM and IPPROTO_IDP parameters:
Once you have created your sockets, you also need to bind() the socket to the interface on which
you want to receive data. This is done exactly the same way as using a TCP connection.
Now that we have our sockets ready, let's take a look at what is required to both send and receive
datagram packets. Remember that even though we have bound the socket, we don't need to call
the listen() or accept() functions, as we are going to be operating in a connectionless manner,
which enables us to send or receive packets to any device on the network.
int sendto (SOCKET s, const char *buf, int len, int flags,
const struct sockaddr *to, int tolen);
sTargetDevice.sin_family = AF_INET;
sTargetDevice.sin_port = htons(40040);
sTargetDevice.sin_addr.s_addr = inet_addr("192.168.0.1");
Notice that the parameters are very similar to those described for the recv() function. The first
parameter, s, is the socket on which we want to receive data. Next, buf is a pointer to a buffer for
the incoming data, and its size is specified by the len parameter. The flags parameter must be set
to 0. Finally, the from parameter contains a pointer to the SOCKADDR_IN structure, which contains
information about the device that sent the data. A pointer to its length is in the fromlen field.
If the packet is received successfully, recvfrom() will return a 0; otherwise, a SOCKET_ERROR will
occur.
The following example shows how to receive a UDP datagram packet:
sReceiveFromAddr.sin_family = AF_INET;
sReceiveFromAddr.sin_port = htons(40040);
sReceiveFromAddr.sin_addr.s_addr = htonl(INADDR_ANY);
One final note regarding the sending and receiving of UDP data: You can also transfer data by using
the connect(), send(), and recv() functions. Transmitting UDP data this way is considered a
somewhat "directed" connectionless transfer, and should be used only if you plan to communicate
with one other device during a session (i.e., all packets are sent to the same address). To do this,
after your UDP socket has been created, call the connect() function with SOCKADDR_IN set to the
machine you want to establish a session with. No real connection will be established, but you can
use the send() and recv() functions to transfer data with the associated address.
Written by EMnify
When higher-level protocols need to transmit data quickly, they rely on UDP to
package and distribute that data over the Internet Protocol (IP).
UDP headers
Like TCP, UDP labels data packets with a header. But UDP headers are
much simpler. There are only four fields:
1. Source port
2. Destination port
3. UDP length
4. Checksum
The source port indicates where the transmission is coming from. The
destination port indicates where it’s going. The UDP length specifies how
many bytes the header and data represent. And the checksum is a field
that can be used to check for errors in the header or data stream.
TCP has numerous additional fields in the header which help verify that the
data packets arrive intact and can be rearranged as needed. These additional
fields significantly increase the size of the header.
UDP in IoT
In IoT (and data transmission in general), User Datagram Protocol is less
common than TCP. But UDP often appeals to IoT manufacturers because it
uses fewer network resources to transmit and doesn’t have to maintain a
constant connection between the two endpoints. In other words, it uses less
data and consumes less power.
UDP security
While UDP is easy to implement and it has less overhead, it also leaves your
devices more vulnerable to cyber attacks.
1. Home
2. Modules
3. Socket
4. Udp-client-server-example
UDP Overview:
UDP is the abbreviation of User Datagram Protocol. UDP makes use of
Internet Protocol of the TCP/IP suit. In communications using UDP, a client
program sends a message packet to a destination server wherein the
destination server also runs on UDP.
Properties of UDP:
The UDP does not provide guaranteed delivery of message packets. If
for some issue in a network if a packet is lost it could be lost forever.
Since there is no guarantee of assured delivery of messages, UDP is
considered an unreliable protocol.
The underlying mechanisms that implement UDP involve no connection-
based communication. There is no streaming of data between a UDP
server or and an UDP Client.
An UDP client can send "n" number of distinct packets to an UDP server
and it could also receive "n" number of distinct packets as replies from
the UDP server.
Since UDP is connectionless protocol the overhead involved in UDP is
less compared to a connection based protocol like TCP.
Example: UDP Server using Python
import socket
localIP = "127.0.0.1"
localPort = 20001
bufferSize = 1024
msgFromServer = "Hello UDP Client"
bytesToSend = str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)
print(clientMsg)
print(clientIP)
# Sending a reply to client
UDPServerSocket.sendto(bytesToSend, address)
Output:
UDP server up and listening
Message from Client:b"Hello UDP Server"
Client IP Address:("127.0.0.1", 51696)