TcpListener 建構函式

定義

初始化 TcpListener 類別的新執行個體。

多載

TcpListener(Int32)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

初始化 TcpListener 類別的新執行個體,這個執行個體包含指定的設計工具。

TcpListener(IPEndPoint)

使用指定的本機端點,初始化 TcpListener 類別的新執行個體。

TcpListener(IPAddress, Int32)

初始化 TcpListener 類別的新執行個體,這個執行個體會在指定的本機 IP 位址和通訊埠編號上接聽連入的連接嘗試。

TcpListener(Int32)

來源:
TCPListener.cs
來源:
TCPListener.cs
來源:
TCPListener.cs

警告

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

警告

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

警告

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

警告

Use TcpListener(IPAddress localaddr, int port).

初始化 TcpListener 類別的新執行個體,這個執行個體包含指定的設計工具。

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)

參數

port
Int32

通訊埠,將會在此處接聽連入的連接嘗試。

屬性

例外狀況

port 不在 MinPortMaxPort 之間。

範例

下列程式碼範例會 TcpListener 使用本機埠號碼建立 。

//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

備註

此建構函式已經過時。 使用 或 TcpListener.TcpListener(IPEndPoint)TcpListener.TcpListener(IPAddress, Int32) 構函式。

此建構函式可讓您指定要接聽連入連線嘗試的埠號碼。 使用此建構函式時,基礎服務提供者會指派最適當的網路位址。 如果您不小心使用哪個本機埠,您可以針對埠號碼指定 0。 在此情況下,服務提供者會指派可用的暫時埠號碼。 如果您使用此方法,您可以使用 屬性來探索已指派 LocalEndpoint 的區域網路位址和埠號碼。

Start呼叫 方法來開始接聽連入連線嘗試。

另請參閱

適用於

TcpListener(IPEndPoint)

來源:
TCPListener.cs
來源:
TCPListener.cs
來源:
TCPListener.cs

使用指定的本機端點,初始化 TcpListener 類別的新執行個體。

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)

參數

localEP
IPEndPoint

IPEndPoint,表示接聽項 (Listener) Socket 所要繫結至的本機端點。

例外狀況

localEPnull

範例

下列程式碼範例會使用本機端點建立 類別的 TcpListener 實例。

//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

備註

此建構函式可讓您指定要接聽連入連線嘗試的本機 IP 位址和埠號碼。 使用此建構函式之前,您必須使用所需的本機 IP 位址和埠號碼來建立 IPEndPoint 。 將此 IPEndPoint 傳遞至建構函式做為 localEP 參數。

如果您不在意指派的本機位址,您可以使用 作為位址參數來建立 IPEndPointIPAddress.Any ,而基礎服務提供者會指派最適當的網路位址。 如果您有多個網路介面,這有助於簡化您的應用程式。 如果您不小心使用哪個本機埠,您可以針對埠號碼建立 IPEndPoint using 0。 在此情況下,服務提供者會指派可用的暫時埠號碼。 如果您使用此方法,您可以使用 屬性來探索已指派 LocalEndpoint 的區域網路位址和埠號碼。

Start呼叫 方法來開始接聽連入連線嘗試。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤

另請參閱

適用於

TcpListener(IPAddress, Int32)

來源:
TCPListener.cs
來源:
TCPListener.cs
來源:
TCPListener.cs

初始化 TcpListener 類別的新執行個體,這個執行個體會在指定的本機 IP 位址和通訊埠編號上接聽連入的連接嘗試。

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)

參數

localaddr
IPAddress

IPAddress,表示本機 IP 位址。

port
Int32

通訊埠,將會在此處接聽連入的連接嘗試。

例外狀況

localaddrnull

port 不在 MinPortMaxPort 之間。

範例

下列程式碼範例會使用本機 IP 位址和埠號碼來建立 類別的 TcpListener 實例。

//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

備註

此建構函式可讓您指定要接聽連入連線嘗試的本機 IP 位址和埠號碼。 呼叫這個建構函式之前,您必須先使用所需的本機位址來建立 IPAddress 。 將此 IPAddress 傳遞至建構函式做為 localaddr 參數。 如果您不在意指派的本機位址,請為 localaddr 參數指定 IPAddress.Any ,而基礎服務提供者會指派最適當的網路位址。 如果您有多個網路介面,這有助於簡化您的應用程式。 如果您不小心使用哪個本機埠,您可以針對埠號碼指定 0。 在此情況下,服務提供者會指派 1024 到 65535 之間的可用埠號碼。 如果您使用此方法,您可以使用 屬性來探索已指派 LocalEndpoint 的區域網路位址和埠號碼。

Start呼叫 方法來開始接聽連入連線嘗試。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤

另請參閱

適用於