UdpClient Constructor (Int32)

 

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
)

Parameters

port
Type: System.Int32

The local port number from which you intend to communicate.

Exception Condition
ArgumentOutOfRangeException

The port parameter is greater than MaxPort or less than MinPort.

SocketException

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. Use this constructor if you are only interested in setting the local port number. The underlying service provider will assign the local IP address. If you pass 0 to the constructor, the underlying service provider will assign a port number. If this constructor is used, the UdpClient instance is set with an address family of IPv4 that cannot be changed or overwritten by a connect method call with an IPv6 target.

System_CAPS_noteNote

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

This constructor works only with IPv4 address types.

The following example demonstrates using a local port number to create an instance of the UdpClient class.

//Creates an instance of the UdpClient class to listen on
// the default interface using a particular port.
try{
         UdpClient udpClient = new UdpClient(11000);
}  
catch (Exception e ) {
          Console.WriteLine(e.ToString());
  }

.NET Framework
Available since 1.1
Return to top
Show: