UDP APIs in .NET
This section provides an in-depth look at the classes and methods available in the .NET Framework for working with User Datagram Protocol (UDP) sockets.
Core UDP Classes
The primary class for UDP communication is System.Net.Sockets.UdpClient
. It provides a high-level interface for sending and receiving UDP datagrams.
UdpClient Class
UdpClient
is a managed wrapper around the underlying socket implementation, simplifying UDP communication.
Constructors
Several constructors are available to initialize a UdpClient
instance:
public UdpClient();
public UdpClient(int port);
public UdpClient(IPEndPoint localEP);
public UdpClient(string hostname, int port);
public UdpClient(AddressFamily addressFamily);
Key Methods
Here are some of the most commonly used methods:
Method | Description |
---|---|
byte[] Receive(ref IPEndPoint remoteEP) | Receives a UDP datagram from a remote host. The remoteEP parameter is an output parameter that will contain the endpoint of the sender. |
int Send(byte[] dgram, int bytes, IPEndPoint remoteEP) | Sends a UDP datagram to a remote host. |
int Send(byte[] dgram, int bytes, string hostname, int port) | Sends a UDP datagram to a remote host specified by hostname and port. |
void Connect(IPEndPoint remoteEP) | Establishes a default remote host for the UdpClient . Subsequent calls to Send without specifying an endpoint will use this default. |
void Connect(string hostname, int port) | Establishes a default remote host using hostname and port. |
byte[] ReceiveAsync().Result | Receives a UDP datagram from a remote host asynchronously. .Result is used here for synchronous retrieval in example context. |
Task |
Sends a UDP datagram to a remote host asynchronously. |
Task |
Receives a UDP datagram from a remote host asynchronously. |
IPEndPoint Class
System.Net.IPEndPoint
represents a network endpoint as an IP address and a port number. It's used extensively with UdpClient
to specify local and remote addresses.
Key Properties
IPAddress Address { get; set; }
: Gets or sets the IP address of the endpoint.int Port { get; set; }
: Gets or sets the port number of the endpoint.
Common UDP Scenarios
Sending a UDP Datagram
To send a datagram, you typically create a UdpClient
, optionally bind it to a local port, and then use the Send
method.
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
public class UdpSender
{
public async Task SendMessageAsync(string message, string ipAddress, int port)
{
using (UdpClient udpClient = new UdpClient())
{
byte[] data = Encoding.UTF8.GetBytes(message);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ipAddress), port);
await udpClient.SendAsync(data, data.Length, remoteEP);
Console.WriteLine($"Sent '{message}' to {remoteEP}");
}
}
}
Receiving UDP Datagrams
To receive datagrams, create a UdpClient
bound to a specific local port and repeatedly call the Receive
or ReceiveAsync
method.
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
public class UdpReceiver
{
public async Task StartReceivingAsync(int port)
{
using (UdpClient udpClient = new UdpClient(port))
{
Console.WriteLine($"Listening for UDP datagrams on port {port}...");
while (true)
{
UdpReceiveResult result = await udpClient.ReceiveAsync();
string receivedMessage = Encoding.UTF8.GetString(result.Buffer);
IPEndPoint remoteEP = result.RemoteEndPoint;
Console.WriteLine($"Received '{receivedMessage}' from {remoteEP}");
// Example: Send a response back
// byte[] responseData = Encoding.UTF8.GetBytes("ACK");
// await udpClient.SendAsync(responseData, responseData.Length, remoteEP);
}
}
}
}
UdpClient
should be bound to a specific local port using a constructor that specifies the port number or an IPEndPoint
.
Considerations for UDP
- Unreliable: UDP does not guarantee delivery, order, or duplicate protection. Implement your own mechanisms if these are required.
- Connectionless: UDP is connectionless, meaning there's no established connection before sending data.
- Low Overhead: Due to its simplicity, UDP has lower overhead than TCP, making it suitable for real-time applications like streaming and gaming.
- Datagram Orientation: Data is sent in discrete packets (datagrams). Each datagram is processed independently.
Example Usage
Here's a simple example demonstrating sending and receiving:
public static async Task Main(string[] args)
{
int listenPort = 11000;
string targetIp = "127.0.0.1"; // Localhost
int targetPort = 11001;
var receiver = new UdpReceiver();
var sender = new UdpSender();
// Start receiver in a separate task
var receiverTask = receiver.StartReceivingAsync(listenPort);
// Give the receiver a moment to start
await Task.Delay(500);
// Send a message
await sender.SendMessageAsync("Hello UDP!", targetIp, targetPort);
// Send another message to a different port (if your receiver also listens there)
await sender.SendMessageAsync("Another UDP message.", targetIp, listenPort);
// To keep the console app running to see output
// await receiverTask; // Uncomment this to keep the receiver running indefinitely
Console.WriteLine("Demo finished. Press Enter to exit.");
Console.ReadLine();
}