MSDN Documentation

User Datagram Protocol (UDP)

The User Datagram Protocol (UDP) is a transport layer protocol that provides a simple, connectionless communication service. Unlike TCP, UDP does not guarantee delivery, order, or duplicate protection of datagrams. It is often used for applications where speed is more critical than reliability, or where the application layer handles reliability mechanisms.

Key Characteristics of UDP:

UDP Header Format:

The UDP header is very small, consisting of only four fields, each 16 bits (2 bytes) in length:

The UDP header is 8 bytes long.

When to Use UDP:

UDP is an excellent choice when:

Comparison with TCP:

Feature UDP TCP
Connection Type Connectionless Connection-oriented
Reliability Unreliable Reliable
Ordering Not guaranteed Guaranteed
Speed Faster Slower
Overhead Low High
Use Cases Streaming, Gaming, DNS, VoIP Web browsing, Email, File transfer

Example UDP Packet Transmission (Conceptual):


// Client sends a UDP packet to a server
// Socket created for UDP communication
Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

// Server's IP address and port
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.100"), 12345);

// Data to send
byte[] sendData = Encoding.ASCII.GetBytes("Hello, UDP Server!");

// Send the data
int bytesSent = udpSocket.SendTo(sendData, serverEndPoint);

// Client might wait for a response (or not, depending on application logic)
byte[] receiveData = new byte[1024];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int bytesReceived = udpSocket.ReceiveFrom(receiveData, ref remoteEndPoint);

string response = Encoding.ASCII.GetString(receiveData, 0, bytesReceived);
Console.WriteLine($"Received from server: {response}");

udpSocket.Close();

// Server receives a UDP packet
// Socket created for UDP communication
Socket udpServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 12345);
udpServerSocket.Bind(localEndPoint);

byte[] receiveBuffer = new byte[1024];
EndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);

int bytesRead = udpServerSocket.ReceiveFrom(receiveBuffer, ref clientEndPoint);
string receivedMessage = Encoding.ASCII.GetString(receiveBuffer, 0, bytesRead);
Console.WriteLine($"Received from client ({clientEndPoint}): {receivedMessage}");

// Server sends a response
byte[] responseData = Encoding.ASCII.GetBytes("UDP Server acknowledges!");
udpServerSocket.SendTo(responseData, clientEndPoint);

udpServerSocket.Close();
            

Note: The UDP checksum is optional for IPv4 but mandatory for IPv6. If the sender does not compute a checksum, it must set the checksum field to zero. If the receiver receives a UDP datagram with a non-zero checksum, it must verify it. If the checksum is invalid, the datagram is typically discarded.

Warning: Applications relying on UDP must implement their own mechanisms for reliability if required. This includes handling packet loss, ensuring data integrity, and managing the order of arrival.

Related Topics: