UdpClient.Client Property

API Summary

Member System.Net.Sockets.UdpClient.Client
Declaration public System.Net.Sockets.Socket Client { get; }
Inheritance Object
Namespace System.Net.Sockets
Assembly System.Net.Primitives

Property Value

A System.Net.Sockets.Socket object associated with the UdpClient.

Remarks

The Client property retrieves the underlying Socket that the UdpClient class uses for network communications. This property is useful if you need to access advanced socket options or perform operations not directly exposed by the UdpClient class.

When you create a new instance of the UdpClient class, a new Socket object is automatically created and associated with it. You can use this property to access that underlying socket.

Modifying the properties of the returned Socket object can affect the behavior of the UdpClient instance. It is generally recommended to use the high-level methods provided by UdpClient unless you have specific advanced requirements.

Example

The following example demonstrates how to access the underlying Socket from a UdpClient to set a specific socket option (e.g., ReuseAddress).


using System;
using System.Net;
using System.Net.Sockets;

public class UdpClientExample
{
    public static void Main()
    {
        try
        {
            // Create a UdpClient instance
            using (UdpClient udpClient = new UdpClient(11000))
            {
                Console.WriteLine("UdpClient created on port 11000.");

                // Access the underlying Socket
                Socket socket = udpClient.Client;

                // Set a socket option: ReuseAddress
                // This allows the socket to be bound to an address that is already in use.
                if (socket != null)
                {
                    int optionValue = 1; // 1 means true
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue);
                    Console.WriteLine("SocketOption ReuseAddress set to true.");

                    // You can also access other socket properties like:
                    Console.WriteLine($"Socket Protocol Type: {socket.ProtocolType}");
                    Console.WriteLine($"Socket Address Family: {socket.AddressFamily}");
                    Console.WriteLine($"Is Socket Connected: {socket.Connected}");

                    // Perform UDP operations using udpClient here...
                    // For example:
                    // byte[] dataToSend = { ... };
                    // udpClient.Send(dataToSend, dataToSend.Length, new IPEndPoint(IPAddress.Loopback, 11001));
                    // byte[] receivedData = udpClient.Receive(ref remoteEP);
                }
                else
                {
                    Console.WriteLine("Failed to retrieve the underlying Socket.");
                }
            }
        }
        catch (SocketException socketEx)
        {
            Console.WriteLine($"SocketException: {socketEx.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}