Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TcpListener.TcpListener(Int32) Constructor

NOTE: This constructor is now obsolete.

Initializes a new instance of the TcpListener class that listens on the specified port.

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

[ObsoleteAttribute("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202")] 
public TcpListener (
	int port
)
/** @attribute ObsoleteAttribute("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202") */ 
public TcpListener (
	int port
)
ObsoleteAttribute("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202") 
public function TcpListener (
	port : int
)
Not applicable.

Parameters

port

The port on which to listen for incoming connection attempts.

Exception typeCondition

ArgumentOutOfRangeException

port is not between MinPort and MaxPort.

This constructor is obsolete. Use the System.Net.Sockets.TcpListener(IPAddress,Int32) or System.Net.Sockets.TcpListener(IPEndPoint) constructors.

This constructor allows you to specify the port number on which to listen for incoming connection attempts. With this constructor, the underlying service provider assigns the most appropriate network address. 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.

The following code example creates a TcpListener using a local port number.

//Creates an instance of the TcpListener class by providing a local port number.  
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
try{
    TcpListener tcpListener =  new TcpListener(ipAddress, 13);    
}
catch ( Exception e ){
    Console.WriteLine( e.ToString());
}


// Creates an instance of the TcpListener class by 
// providing a local port number.  
IPAddress ipAddress = (IPAddress)Dns.Resolve(
    "localhost").get_AddressList().get_Item(0);
try {
    TcpListener tcpListener = 
        new TcpListener(ipAddress, 13);
}
catch (System.Exception e) {
    Console.WriteLine(e.ToString());
}


Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 1.0, 1.1, 2.0
Obsolete (compiler warning) in 3.0

.NET Compact Framework

Supported in: 1.0
Obsolete (compiler warning) in 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft