UdpClient Class

Namespace: System.Net.Sockets
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.

public UdpClient()

UdpClient(int port)

Initializes a new instance of the UdpClient class and binds it to the specified local port.

public UdpClient(int port)
Parameters:
  • port: An integer that specifies the local port to bind the UdpClient to.

UdpClient(IPEndPoint localEP)

Initializes a new instance of the UdpClient class and binds it to the specified local endpoint.

public UdpClient(IPEndPoint localEP)
Parameters:
  • localEP: An IPEndPoint that specifies the local endpoint to bind the UdpClient to.

Methods

Send(byte[] datagram, int bytes, IPEndPoint remoteEP)

Sends UDP data to a specified network endpoint.

public int Send(byte[] datagram, int bytes, IPEndPoint remoteEP)
Parameters:
  • 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.
Returns: An integer representing the number of bytes sent.
Exceptions:
  • ArgumentNullException: If datagram is null.
  • ArgumentOutOfRangeException: If bytes is less than zero or greater than the length of datagram.
  • SocketException: If a socket error occurs.

Receive(ref IPEndPoint remoteEP)

Receives a UDP datagram from a remote host.

public byte[] Receive(ref IPEndPoint remoteEP)
Parameters:
  • remoteEP: An IPEndPoint that will contain the endpoint of the remote host.
Returns: A byte array containing the received UDP data.
Exceptions:
  • SocketException: If a socket error occurs.

Close()

Closes the UdpClient connection.

public void Close()

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}");
        }
    }
}