UdpClient Constructor (Int32, AddressFamily)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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.

Exception Condition

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 this constructor is used, the UdpClient instance is set with the address family specified by the family parameter that cannot be changed or overwritten by a connect method call with a different address family.


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
Available since 1.1
Return to top