Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Net.Sockets
TcpListener Class
 TcpListener Constructor (IPEndPoint...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
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)

Visual Basic (Declaration)
Public Sub New ( _
    localEP As IPEndPoint _
)
Visual Basic (Usage)
Dim localEP As IPEndPoint

Dim instance As New TcpListener(localEP)
C#
public TcpListener (
    IPEndPoint localEP
)
C++
public:
TcpListener (
    IPEndPoint^ localEP
)
J#
public TcpListener (
    IPEndPoint localEP
)
JScript
public function TcpListener (
    localEP : IPEndPoint
)

Parameters

localEP

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

Exception typeCondition

ArgumentNullException

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

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.

Visual Basic
'Creates an instance of the TcpListener class by providing a local endpoint.
Dim ipAddress As IPAddress = Dns.Resolve(Dns.GetHostName()).AddressList(0)
Dim ipLocalEndPoint As New IPEndPoint(ipAddress, 11000)

Try
   Dim tcpListener As New TcpListener(ipLocalEndPoint)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try
C#
//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());
}
C++
//Creates an instance of the TcpListener class by providing a local endpoint.

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

try
{
   TcpListener^ tcpListener = gcnew TcpListener( ipLocalEndPoint );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
J#
// Creates an instance of the TcpListener class by providing a 
// local endpoint.
IPAddress ipAddress = (IPAddress)Dns.Resolve(
    Dns.GetHostName()).get_AddressList().get_Item(0);
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
try {
    TcpListener tcpListener = new TcpListener(ipLocalEndPoint);
}
catch (System.Exception e) {
    Console.WriteLine(e.ToString());
}

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker