UdpClient Class
Provides a User Datagram Protocol (UDP) client that can send and receive datagrams.
Syntax
public class UdpClient
: IDisposable
Remarks
The UdpClient class provides a simple interface for sending and receiving UDP packets. It handles the underlying socket creation and management for you, simplifying UDP communication. This class is useful for scenarios where you need to broadcast messages, implement protocols that do not require reliable ordered delivery (like DNS or NTP), or create applications that communicate with devices over a network using UDP.
When you create a UdpClient object, it can be bound to a specific local port or to any available port. You can use the Connect method to establish a default remote endpoint, or you can specify the remote endpoint when sending data using the Send method. Similarly, you can receive data using Receive, which will return data from any endpoint, or use ReceiveAsync for asynchronous operations.
UdpClient does not establish or maintain a persistent connection with a remote host. Each datagram is sent independently and may arrive out of order, be duplicated, or be lost entirely. If reliable ordered delivery is required, consider using the TcpClient class.
Constructors
UdpClient()
public UdpClient()
Initializes a new instance of the UdpClient class. This constructor binds the client to a random local port.
UdpClient(int port)
public UdpClient(int port)
Initializes a new instance of the UdpClient class and binds it to the specified local port. If the specified port is 0, the UdpClient will be bound to a random port.
port: The local port to bind to.
UdpClient(int port, IPAddress localAddress)
public UdpClient(int port, IPAddress localAddress)
Initializes a new instance of the UdpClient class and binds it to the specified local port and IP address. If the specified port is 0, the UdpClient will be bound to a random port.
port: The local port to bind to.localAddress: The local IP address to bind to.
Methods
Send(byte[] datagram, int bytes, IPEndPoint endPoint)
public int Send(byte[] datagram, int bytes, IPEndPoint endPoint)
Sends UDP data to a remote device. The data is sent from the local client to the specified remote device.
datagram: Abytearray containing the data to be sent.bytes: The number of bytes in the datagram parameter.endPoint: AnIPEndPointobject that specifies the remote host and port to send the data to.
Socket.
Receive(ref IPEndPoint remoteEP)
public byte[] Receive(ref IPEndPoint remoteEP)
Receives UDP data from a remote device. This method blocks until a datagram is received.
remoteEP: AnIPEndPointobject that contains the address and port of the remote device sending the data.
byte array containing the data received from the remote device.
Close()
public void Close()
Closes the UdpClient connection.
Examples
The following example demonstrates sending a UDP message and then receiving a response:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpExample
{
public static void Main(string[] args)
{
// Define remote endpoint
IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // Replace with actual IP if needed
int remotePort = 11000;
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, remotePort);
// Create a UdpClient
using (UdpClient client = new UdpClient())
{
// Send a message
string message = "Hello, UDP!";
byte[] sendBytes = Encoding.ASCII.GetBytes(message);
client.Send(sendBytes, sendBytes.Length, remoteEndPoint);
Console.WriteLine($"Sent: {message} to {remoteEndPoint}");
// Receive a response (optional, depends on server logic)
IPEndPoint receivedFromEndPoint = null;
byte[] receivedBytes = client.Receive(ref receivedFromEndPoint);
string receivedMessage = Encoding.ASCII.GetString(receivedBytes);
Console.WriteLine($"Received: {receivedMessage} from {receivedFromEndPoint}");
}
}
}