Επεξεργασία

Κοινή χρήση μέσω


TcpListener Constructors

Definition

Initializes a new instance of the TcpListener class.

Overloads

TcpListener(Int32)
Obsolete.
Obsolete.
Obsolete.
Obsolete.

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

TcpListener(IPEndPoint)

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

TcpListener(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.

TcpListener(Int32)

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

Caution

This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. https://go.microsoft.com/fwlink/?linkid=14202

Caution

This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.

Caution

This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202

Caution

Use TcpListener(IPAddress localaddr, int port).

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

public:
 TcpListener(int port);
[System.Obsolete("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. https://go.microsoft.com/fwlink/?linkid=14202")]
public TcpListener (int port);
[System.Obsolete("This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.")]
public TcpListener (int port);
[System.Obsolete("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public TcpListener (int port);
[System.Obsolete("Use TcpListener(IPAddress localaddr, int port).")]
public TcpListener (int port);
[<System.Obsolete("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. https://go.microsoft.com/fwlink/?linkid=14202")>]
new System.Net.Sockets.TcpListener : int -> System.Net.Sockets.TcpListener
[<System.Obsolete("This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.")>]
new System.Net.Sockets.TcpListener : int -> System.Net.Sockets.TcpListener
[<System.Obsolete("This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202")>]
new System.Net.Sockets.TcpListener : int -> System.Net.Sockets.TcpListener
[<System.Obsolete("Use TcpListener(IPAddress localaddr, int port).")>]
new System.Net.Sockets.TcpListener : int -> System.Net.Sockets.TcpListener
Public Sub New (port As Integer)

Parameters

port
Int32

The port on which to listen for incoming connection attempts.

Attributes

Exceptions

port is not between MinPort and MaxPort.

Examples

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 = gcnew 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 = 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.  
   Dim ipAddress As IPAddress = Dns.Resolve("localhost").AddressList(0)
   Try
    Dim tcpListener As New TcpListener(ipAddress, 13)
   Catch e As Exception
      Console.WriteLine(e.ToString())
   End Try

Remarks

This constructor is obsolete. Use the TcpListener.TcpListener(IPAddress, Int32) or TcpListener.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 ephemeral port number. 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.

See also

Applies to

TcpListener(IPEndPoint)

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

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

public:
 TcpListener(System::Net::IPEndPoint ^ localEP);
public TcpListener (System.Net.IPEndPoint localEP);
new System.Net.Sockets.TcpListener : System.Net.IPEndPoint -> System.Net.Sockets.TcpListener
Public Sub New (localEP As IPEndPoint)

Parameters

localEP
IPEndPoint

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

Exceptions

localEP is null.

Examples

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 = gcnew IPEndPoint( ipAddress,11000 );

try
{
   TcpListener^ tcpListener = gcnew 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());
}
'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

Remarks

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 ephemeral port number. 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.

Note

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

See also

Applies to

TcpListener(IPAddress, Int32)

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

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

public:
 TcpListener(System::Net::IPAddress ^ localaddr, int port);
public TcpListener (System.Net.IPAddress localaddr, int port);
new System.Net.Sockets.TcpListener : System.Net.IPAddress * int -> System.Net.Sockets.TcpListener
Public Sub New (localaddr As IPAddress, port As Integer)

Parameters

localaddr
IPAddress

An IPAddress that represents the local IP address.

port
Int32

The port on which to listen for incoming connection attempts.

Exceptions

localaddr is null.

port is not between MinPort and MaxPort.

Examples

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

//Creates an instance of the TcpListener class by providing a local IP address and port number.

IPAddress^ ipAddress = Dns::Resolve( "localhost" )->AddressList[ 0 ];

try
{
   TcpListener^ tcpListener = gcnew TcpListener( ipAddress,13 );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
//Creates an instance of the TcpListener class by providing a local IP address and 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 IP address and port number.
   Dim ipAddress As IPAddress = Dns.Resolve("localhost").AddressList(0)
   
   Try
      Dim tcpListener As New TcpListener(ipAddress, 13)
   Catch e As Exception
      Console.WriteLine(e.ToString())
   End Try

Remarks

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 65535. 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.

Note

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

See also

Applies to