UdpClient Class

Namespace: System.Net.Sockets

Assembly: System.dll

Summary

Provides UDP network services.

Syntax


public class UdpClient : IDisposable
            

Remarks

The UdpClient class provides access to the UDP network protocol, which is a connectionless protocol that sends datagrams. UDP is a connectionless protocol that does not guarantee delivery or order of datagrams. It is a lightweight protocol that is suitable for applications that require speed and do not require guaranteed delivery, such as streaming media or online gaming.

Use the UdpClient class to:

The UdpClient class implements the IDisposable interface, so you should call the Dispose method when you are finished with it to release any unmanaged resources.

Constructors

UdpClient()

Initializes a new instance of the UdpClient class.


// Example
var udpClient = new UdpClient();
            

UdpClient(int port)

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


// Example
var udpClient = new UdpClient(11000);
            

UdpClient(IPEndPoint localEP)

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


// Example
var ipEndpoint = new IPEndPoint(IPAddress.Any, 11000);
var udpClient = new UdpClient(ipEndpoint);
            

Properties

Client

Gets or sets the underlying Socket object.


public Socket Client { get; set; }
            

DontFragment

Gets or sets a value that indicates whether the Don't Fragment bit is set in the UDP header.


public bool DontFragment { get; set; }
            

MulticastLoopback

Gets or sets a value that indicates whether multicast packets sent by this UdpClient are looped back on the local network interface.


public bool MulticastLoopback { get; set; }
            

Methods

Send(byte[] dgram, int bytes, IPEndPoint endPoint)

Sends UDP datagram packets to a remote host.


public int Send(byte[] dgram, int bytes, IPEndPoint endPoint);
            

Parameters:

Name Type Description
dgram byte[] The buffer containing the data to send.
bytes int The number of bytes to send.
endPoint IPEndPoint The IPEndPoint indicating the remote host to which to send the datagram.

Receive(ref IPEndPoint remoteEP)

Receives UDP datagram packets from a remote host.


public byte[] Receive(ref IPEndPoint remoteEP);
            

Parameters:

Name Type Description
remoteEP ref IPEndPoint An IPEndPoint object that contains the remote host's IPAddress and port number.

Returns:

A byte array containing the UDP datagram.

JoinMulticastGroup(IPAddress multicastAddr)

Joins the specified multicast group.


public void JoinMulticastGroup(IPAddress multicastAddr);
            

DropMulticastGroup(IPAddress multicastAddr)

Leaves the specified multicast group.


public void DropMulticastGroup(IPAddress multicastAddr);
            

Dispose()

Releases the unmanaged resources used by the UdpClient and optionally releases the managed resources.


public void Dispose();
            

Events

The UdpClient class does not expose any public events.

Exceptions

Various exceptions can be thrown by methods of the UdpClient class, including:

Example

The following example demonstrates how to send and receive a UDP message using the UdpClient class.


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

public class UdpExample
{
    public static async Task Main(string[] args)
    {
        int port = 11000;
        var udpClient = new UdpClient(port);
        var remoteEP = new IPEndPoint(IPAddress.Loopback, port);

        Console.WriteLine($"UDP server listening on port {port}...");

        // Send a message
        string messageToSend = "Hello, UDP!";
        byte[] sendBytes = Encoding.ASCII.GetBytes(messageToSend);
        await udpClient.SendAsync(sendBytes, sendBytes.Length, remoteEP);
        Console.WriteLine($"Sent: {messageToSend}");

        // Receive a message
        var receivedResult = await udpClient.ReceiveAsync();
        string receivedMessage = Encoding.ASCII.GetString(receivedResult.Buffer);
        IPEndPoint senderEP = receivedResult.RemoteEndPoint;

        Console.WriteLine($"Received from {senderEP}: {receivedMessage}");

        udpClient.Close();
        Console.WriteLine("UDP client closed.");
    }
}
            

See Also