UdpClient Class
Provides UDP network services.
Namespace: System.Net.Sockets
Assembly: System.Net.Primitives.dll
Inheritance: Object → MarshalByRefObject → Component → UdpClient
Remarks
The UdpClient class provides a simple client for sending and receiving datagrams using UDP. A datagram is a variable-length chunk of data. UDP is a connectionless protocol, which means that a connection is not established before sending data. This makes UDP suitable for applications that require fast, low-overhead, or real-time transmissions. However, UDP does not guarantee that datagrams will arrive in the order they were sent, or that they will arrive at all.
The UdpClient class handles the details of UDP socket programming, allowing you to focus on the application logic. It provides methods for sending and receiving datagrams, specifying remote and local endpoints, and managing socket options.
Constructors
UdpClient(): Initializes a new instance of theUdpClientclass.UdpClient(Int32): Initializes a new instance of theUdpClientclass and binds it to the specified local port.UdpClient(IPEndPoint): Initializes a new instance of theUdpClientclass and binds it to the specified local endpoint.UdpClient(string, Int32): Initializes a new instance of theUdpClientclass and binds it to the specified port on the specified host.
Properties
Client: Gets or sets the underlying Socket.ExclusiveAddressUse: Gets or sets a value that indicates whether theUdpClientshould attempt to bind exclusively to the specified port.Client.IsBound: Gets a value indicating whether theSocketis bound.
Methods
Connect(IPEndPoint): Connects the UDP client to a remote host.Connect(string, int): Connects the UDP client to a remote host.ReceiveAsync(): Asynchronously receives a datagram from the network.SendAsync(byte[], int, IPEndPoint): Asynchronously sends data to a remote host.Close(): Releases all resources used by theUdpClient.Dispose(): Releases all resources used by theUdpClient.
Example
Basic UDP Sender and Receiver
This example demonstrates how to create a simple UDP sender and receiver using UdpClient.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
public class UdpExample
{
public static async Task Run()
{
// --- Receiver ---
var receiver = new UdpClient(11000); // Listen on port 11000
Console.WriteLine("UDP Receiver started on port 11000.");
Task.Run(async () =>
{
while (true)
{
UdpReceiveResult result = await receiver.ReceiveAsync();
string message = Encoding.ASCII.GetString(result.Buffer);
Console.WriteLine($"Received from {result.RemoteEndPoint}: {message}");
}
});
// --- Sender ---
var sender = new UdpClient();
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 11000); // Send to localhost on port 11000
Console.WriteLine("Enter messages to send (type 'exit' to quit):");
string input;
while ((input = Console.ReadLine()) != "exit")
{
byte[] data = Encoding.ASCII.GetBytes(input);
await sender.SendAsync(data, data.Length, remoteEndPoint);
Console.WriteLine($"Sent: {input}");
}
sender.Close();
receiver.Close();
Console.WriteLine("Sender and Receiver closed.");
}
}