User Datagram Protocol (UDP)
The User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite (the set of network protocols used for the Internet). With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without prior communications to set up special transmission channels or data paths. The protocol was designed by David P. Reed in 1980 and formally defined in RFC 768.
UDP uses a simple transmission model with a minimum of protocol mechanism. It has no handshaking dialogues, and thus exposes any unreliability of the underlying network protocol to the user's program. As this is normally IP over unreliable media, there is no guarantee of delivery, ordering, or duplicate protection. UDP provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram.
UDP is suitable for purposes where error checking and correction is either not necessary or performed in the application, avoiding the overhead of such processing at the network interface level. Time-sensitive applications often use UDP because dropping packets is preferable to waiting for delayed packets, which may not be an option in a real-time system. If error correction facilities are needed at the network interface level, an application may use the Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP) which are designed for this purpose.
A number of UDP's attributes make it especially suited for certain applications
• It is transaction-oriented, suitable for simple query-response protocols such as the Domain Name System or the Network Time Protocol.
• It provides datagrams, suitable for modeling other protocols such as in IP tunneling or Remote Procedure Call and the Network File System.
• It is simple, suitable for bootstrapping or other purposes without a full protocol stack, such as the DHCP and Trivial File Transfer Protocol.
• It is stateless, suitable for very large numbers of clients, such as in streaming media applications for example IPTV
• The lack of retransmission delays makes it suitable for real-time applications such as Voice over IP, online games, and many protocols built on top of the Real Time Streaming Protocol.
Works well in unidirectional communication, suitable for broadcast information such as in many kinds of service discovery and shared information such as broadcast time or Routing Information Protocol.
Why we use UDP Protocol?
We use the User Datagram Protocol (UDP) protocol when speed and low latency are more important than guaranteed delivery.
Unlike TCP, UDP is:
• connectionless
• lightweight
• fast
• low overhead
It sends data without establishing a connection or waiting for acknowledgments.
1. Faster communication
UDP does not:
• establish a connection
• retransmit lost packets
• wait for acknowledgments
• reorder packets
Because of this, it has very low latency.
This is important for:
• real-time systems
• live communication
• fast interactive apps
2. Low overhead
UDP headers are very small:
• UDP header = 8 bytes
• TCP header = at least 20 bytes
Less overhead means:
• faster transmission
• lower bandwidth usage
3. Real-time applications
In some applications, old data is useless.
Example:
In a video call, if one frame is lost:
• it’s better to skip it
• instead of waiting for retransmission
Otherwise the call freezes or lags.
Key Features of UDP
1. Connectionless
No handshake or setup—data is sent immediately.
2. Unreliable Delivery
No acknowledgments, retransmissions, or guarantees.
3. No Ordering
Packets may arrive out of order—or not at all.
4. Low Overhead
Very small header size, making it lightweight and fast.
5. Stateless Communication
Each packet is independent; no session tracking.
C# Examples of UDP Protocol
1. UDP Server Example
This server:
• listens on port 5000
• waits for UDP messages
• prints received data
• sends a reply back
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UdpServer
{
static void Main()
{
UdpClient server = new UdpClient(5000);
Console.WriteLine("UDP Server started...");
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
byte[] receivedBytes =
server.Receive(ref clientEndPoint);
string message =
Encoding.UTF8.GetString(receivedBytes);
Console.WriteLine(
$"Received from {clientEndPoint}: {message}"
);
string response = "Message received";
byte[] responseBytes =
Encoding.UTF8.GetBytes(response);
server.Send(
responseBytes,
responseBytes.Length,
clientEndPoint
);
}
}
}
2. UDP Client Example
This client:
• sends a UDP message
• waits for the server response
using System;
using System.Net.Sockets;
using System.Text;
class UdpClientExample
{
static void Main()
{
UdpClient client = new UdpClient();
string message = "Hello UDP Server";
byte[] data =
Encoding.UTF8.GetBytes(message);
client.Send(data, data.Length, "127.0.0.1", 5000);
Console.WriteLine("Message sent");
var remoteEndPoint =
new System.Net.IPEndPoint(
System.Net.IPAddress.Any,
0
);
byte[] receivedBytes =
client.Receive(ref remoteEndPoint);
string response =
Encoding.UTF8.GetString(receivedBytes);
Console.WriteLine("Server says: " + response);
client.Close();
}
}
Advantages of UDP
• Very fast: No connection setup or waiting for ACKs
• Low latency: Ideal for real-time communication
• Efficient: Minimal bandwidth and processing overhead
• Simple protocol: Easier to implement
Disadvantages of UDP
• No reliability: Packets can be lost without notice
• No error recovery: Lost data is not retransmitted
• No ordering guarantee
• Not suitable for critical data (e.g., file transfers)
Basic UDP Flow
UDP communication is simple:
• Sender creates datagram
• Datagram sent to IP + port
• Receiver reads it
No handshake is required.
UDP vs TCP (Quick Contrast)
Compared with Transmission Control Protocol:
• UDP → fast, lightweight, unreliable
• TCP → slower, heavier, reliable
Comparison of UDP and TCP
| Feature | TCP | UDP |
|---|---|---|
| Full Name | Transmission Control Protocol | User Datagram Protocol |
| Connection Type | Connection-oriented | Connectionless |
| Reliability | Reliable | Unreliable |
| Packet Ordering | Guaranteed | Not guaranteed |
| Error Checking | Yes | Basic only |
| Retransmission | Yes | No |
| Speed | Slower | Faster |
| Header Size | 20+ bytes | 8 bytes |
| Flow Control | Yes | No |
| Congestion Control | Yes | No |
| Data Type | Stream-based | Packet-based |
| Connection Setup | 3-way handshake | No handshake |
| Broadcast Support | No | Yes |
| Typical Usage | Websites, Email, File Transfer | Gaming, Streaming, VoIP, DNS |
| Examples | HTTP, HTTPS, FTP, SMTP | DNS, DHCP, Online Games |