Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
TcpListener Constructor (IPAddress, Int32)

TcpListener Constructor (IPAddress, Int32)

Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number.

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

new : 
        localaddr:IPAddress * 
        port:int -> TcpListener

Parameters

localaddr
Type: System.Net.IPAddress

An IPAddress that represents the local IP address.

port
Type: System.Int32

The port on which to listen for incoming connection attempts.

ExceptionCondition
ArgumentNullException

localaddr is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

port is not between MinPort and MaxPort.

This constructor allows you to specify the local IP address and port number on which to listen for incoming connection attempts. Before calling this constructor you must first create an IPAddress using the desired local address. Pass this IPAddress to the constructor as the localaddr parameter. If you do not care which local address is assigned, specify IPAddress.Any for the localaddr parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can specify 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000. If you use this approach, you can discover what local network address and port number has been assigned by using the LocalEndpoint property.

Call the Start method to begin listening for incoming connection attempts.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

The following code example creates an instance of the TcpListener class using a local IP address and port number.

No code example is currently available or this language may not be supported.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft