UdpClient Constructor (Int32, AddressFamily)

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

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public UdpClient(
	int port,
	AddressFamily family


Type: System.Int32
The port on which to listen for incoming connection attempts.
Type: System.Net.Sockets.AddressFamily
One of the AddressFamily values that specifies the addressing scheme of the socket.


family is not InterNetwork or InterNetworkV6.


port is greater than MaxPort or less than MinPort.


An error occurred when accessing the socket. See the Remarks section for more information.

This constructor creates an underlying Socket and binds it to the port number from which you intend to communicate.

The family parameter determines whether the listener uses an IP version 4 address (IPv4) or an IP version 6 (IPv6) address. To use an IPv4 address, pass the InterNetwork value. To use an IPv6 address, pass the InterNetworkV6 value. Passing any other value will cause the method to throw an ArgumentException.


If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error.

The following code example shows how to create a UDP client to use in a multicast group.

        // Bind and listen on port 2000. This constructor creates a socket 
        // and binds it to the port on which to receive data. The family 
        // parameter specifies that this connection uses an IPv6 address.
        clientOriginator = new UdpClient(2000, AddressFamily.InterNetworkV6);

        // Join or create a multicast group. The multicast address ranges 
        // to use are specified in RFC#2375. You are free to use 
        // different addresses.

        // Transform the string address into the internal format.
        m_GrpAddr = IPAddress.Parse("FF01::1");

        // Display the multicast address used.
        Console.WriteLine("Multicast Address: [" + m_GrpAddr.ToString() + "]");

        // Exercise the use of the IPv6MulticastOption.
        Console.WriteLine("Instantiate IPv6MulticastOption(IPAddress)");

        // Instantiate IPv6MulticastOption using one of the 
        // overloaded constructors.
        IPv6MulticastOption ipv6MulticastOption = new IPv6MulticastOption(m_GrpAddr);

        // Store the IPAdress multicast options.
        IPAddress group =  ipv6MulticastOption.Group;
        long interfaceIndex = ipv6MulticastOption.InterfaceIndex;

        // Display IPv6MulticastOption properties.
        Console.WriteLine("IPv6MulticastOption.Group: [" + group  + "]");
        Console.WriteLine("IPv6MulticastOption.InterfaceIndex: [" + interfaceIndex + "]");

        // Instantiate IPv6MulticastOption using another 
        // overloaded constructor.
        IPv6MulticastOption ipv6MulticastOption2 = new IPv6MulticastOption(group, interfaceIndex);

        // Store the IPAdress multicast options.
        group =  ipv6MulticastOption2.Group;
        interfaceIndex = ipv6MulticastOption2.InterfaceIndex;

        // Display the IPv6MulticastOption2 properties.
        Console.WriteLine("IPv6MulticastOption.Group: [" + group  + "]");
        Console.WriteLine("IPv6MulticastOption.InterfaceIndex: [" + interfaceIndex + "]");

        // Join the specified multicast group using one of the 
        // JoinMulticastGroup overloaded methods.
        clientOriginator.JoinMulticastGroup((int)interfaceIndex, group);

        // Define the endpoint data port. Note that this port number
        // must match the ClientTarget UDP port number which is the
        // port on which the ClientTarget is receiving data.
        m_ClientTargetdest = new IPEndPoint(m_GrpAddr, 1000);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft