UdpClient Class
Assembly: System.Net.Sockets.dll
Provides UDP network services.
Syntax
public class UdpClient : IDisposable
Remarks
The UdpClient class provides access to the User Datagram Protocol (UDP) functionality. UDP is a connectionless protocol that provides a datagram service. It is a faster but less reliable protocol than TCP. When you use UdpClient, you do not need to establish a connection with the remote host before sending or receiving data.
UdpClient provides methods for sending and receiving datagrams. The Send method sends a datagram to a specified remote host and port. The Receive method waits for an incoming datagram and returns it. You can specify a default remote host and port for the client, in which case you only need to provide the data to send or receive.
The UdpClient class also supports asynchronous operations through the use of the BeginSend, EndSend, BeginReceive, and EndReceive methods. These methods allow you to perform network operations without blocking the main thread of your application.
Constructors
UdpClient()
Initializes a new instance of the UdpClient class.
UdpClient(int port)
Initializes a new instance of the UdpClient class and binds it to the specified local port.
port: An integer that specifies the local port to bind theUdpClientto.
UdpClient(IPEndPoint localEP)
Initializes a new instance of the UdpClient class and binds it to the specified local endpoint.
localEP: An IPEndPoint that specifies the local endpoint to bind theUdpClientto.
Methods
Send(byte[] datagram, int bytes, IPEndPoint remoteEP)
Sends UDP data to a specified network endpoint.
datagram: A byte array containing the UDP data to send.bytes: The number of bytes to send from the datagram.remoteEP: An IPEndPoint that specifies the network endpoint to send the data to.
ArgumentNullException: Ifdatagramis null.ArgumentOutOfRangeException: Ifbytesis less than zero or greater than the length ofdatagram.SocketException: If a socket error occurs.
Receive(ref IPEndPoint remoteEP)
Receives a UDP datagram from a remote host.
remoteEP: An IPEndPoint that will contain the endpoint of the remote host.
SocketException: If a socket error occurs.
Close()
Closes the UdpClient connection.
Example
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpExample
{
public static void Main(string[] args)
{
// Sender
try
{
using (UdpClient sender = new UdpClient())
{
IPEndPoint targetEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
string message = "Hello UDP!";
byte[] datagram = Encoding.ASCII.GetBytes(message);
sender.Send(datagram, datagram.Length, targetEP);
Console.WriteLine($"Sent: {message} to {targetEP}");
}
}
catch (Exception e)
{
Console.WriteLine($"Sender error: {e.Message}");
}
// Receiver
try
{
using (UdpClient receiver = new UdpClient(11000)) // Listen on port 11000
{
Console.WriteLine("Waiting for UDP datagrams...");
IPEndPoint remoteEP = null;
byte[] receivedBytes = receiver.Receive(ref remoteEP);
string receivedMessage = Encoding.ASCII.GetString(receivedBytes);
Console.WriteLine($"Received: {receivedMessage} from {remoteEP}");
}
}
catch (Exception e)
{
Console.WriteLine($"Receiver error: {e.Message}");
}
}
}