TcpListener Constructor (IPEndPoint)

Initializes a new instance of the TcpListener class with the specified local endpoint.

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

public TcpListener(
	IPEndPoint localEP
)

Parameters

localEP
Type: System.Net.IPEndPoint

An IPEndPoint that represents the local endpoint to which to bind the listener Socket.

ExceptionCondition
ArgumentNullException

localEP is null.

This constructor allows you to specify the local IP address and port number on which to listen for incoming connection attempts. Before using this constructor, you must create an IPEndPoint using the desired local IP address and port number. Pass this IPEndPoint to the constructor as the localEP parameter.

If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress.Any as the address 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 create an IPEndPoint using 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.

The following code example creates an instance of the TcpListener class using the local endpoint.

//Creates an instance of the TcpListener class by providing a local endpoint.

IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); 

try{
    TcpListener tcpListener = new TcpListener(ipLocalEndPoint);
}
catch ( Exception e ){
    Console.WriteLine( e.ToString());
}
//Creates an instance of the TcpListener class by providing a local endpoint.

IPAddress* ipAddress = Dns::Resolve(Dns::GetHostName())->AddressList[0];
IPEndPoint* ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); 

try{
    TcpListener* tcpListener = new TcpListener(ipLocalEndPoint);
}
catch ( Exception* e ){
    Console::WriteLine( e->ToString());
}

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

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

.NET Compact Framework

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

Community Additions

ADD
Show:
© 2014 Microsoft