TcpClient.Connect 方法

定義

使用指定的主機名稱和連接埠號碼將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified host name and port number.

多載

Connect(IPEndPoint)

使用指定的遠端網路端點將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified remote network endpoint.

Connect(IPAddress, Int32)

使用指定的 IP 位址和通訊埠編號將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified IP address and port number.

Connect(IPAddress[], Int32)

使用指定的 IP 位址和通訊埠編號,將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified IP addresses and port number.

Connect(String, Int32)

將用戶端連接到所指定主機上的指定連接埠。Connects the client to the specified port on the specified host.

Connect(IPEndPoint)

使用指定的遠端網路端點將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified remote network endpoint.

public:
 void Connect(System::Net::IPEndPoint ^ remoteEP);
public void Connect (System.Net.IPEndPoint remoteEP);
member this.Connect : System.Net.IPEndPoint -> unit
Public Sub Connect (remoteEP As IPEndPoint)

參數

remoteEP
IPEndPoint

您想連接到的 IPEndPointThe IPEndPoint to which you intend to connect.

例外狀況

remoteEp 參數為 nullThe remoteEp parameter is null.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

TcpClient 已關閉。The TcpClient is closed.

範例

下列程式IPEndPoint代碼範例會使用來連接遠端主機。The following code example uses an IPEndPoint to connect with a remote host.

//Uses a remote end point to establish a socket connection.
TcpClient^ tcpClient = gcnew TcpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddress,11004 );
tcpClient->Connect( ipEndPoint );

//Uses a remote endpoint to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint (ipAddress, 11004);

tcpClient.Connect (ipEndPoint);

'Uses a remote endpoint to establish a socket connection.
Dim tcpClient As New TcpClient
Dim ipAddress As IPAddress = Dns.GetHostEntry("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)

tcpClient.Connect(ipEndPoint)

備註

呼叫這個方法,以建立與指定IPEndPoint之的同步遠端主機連接。Call this method to establish a synchronous remote host connection to the specified IPEndPoint. 在呼叫Connect之前,您必須使用 IP 位址和埠IPEndPoint號碼來建立類別的實例。Before you call Connect, you must create an instance of the IPEndPoint class using an IP address and a port number. 請使用IPEndPoint這個remoteEP做為參數。Use this IPEndPoint as the remoteEP parameter. Connect方法將會封鎖,直到連接或失敗為止。The Connect method will block until it either connects or fails. 連接遠端主機之後,請使用GetStream方法來取得基礎。 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此NetworkStream來傳送和接收資料。Use this NetworkStream to send and receive data.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework

另請參閱

Connect(IPAddress, Int32)

使用指定的 IP 位址和通訊埠編號將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified IP address and port number.

public:
 void Connect(System::Net::IPAddress ^ address, int port);
public void Connect (System.Net.IPAddress address, int port);
member this.Connect : System.Net.IPAddress * int -> unit
Public Sub Connect (address As IPAddress, port As Integer)

參數

address
IPAddress

您想連接到的主機的 IPAddressThe IPAddress of the host to which you intend to connect.

port
Int32

您想連接的通訊埠編號。The port number to which you intend to connect.

例外狀況

address 參數為 nullThe address parameter is null.

port 不在 MinPortMaxPort 之間。The port is not between MinPort and MaxPort.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列程式碼範例會使用 IP 位址和埠號碼來連接遠端主機。The following code example uses an IP Address and port number to connect with a remote host.

//Uses the IP address and port number to establish a socket connection.
TcpClient^ tcpClient = gcnew TcpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
tcpClient->Connect( ipAddress, 11003 );

//Uses the IP address and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];

tcpClient.Connect (ipAddress, 11003);

'Uses the IP address and port number to establish a socket connection.
Dim tcpClient As New TcpClient
Dim ipAddress As IPAddress = Dns.GetHostEntry("www.contoso.com").AddressList(0)
tcpClient.Connect(ipAddress, 11003)

備註

呼叫這個方法,以建立與指定IPAddress之和埠號碼的同步遠端主機連接。Call this method to establish a synchronous remote host connection to the specified IPAddress and port number. Connect方法將會封鎖,直到連接或失敗為止。The Connect method will block until it either connects or fails. 連接遠端主機之後,請使用GetStream方法來取得基礎。 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此NetworkStream來傳送和接收資料。Use this NetworkStream to send and receive data.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

另請參閱

Connect(IPAddress[], Int32)

使用指定的 IP 位址和通訊埠編號,將用戶端連接至遠端 TCP 主機。Connects the client to a remote TCP host using the specified IP addresses and port number.

public:
 void Connect(cli::array <System::Net::IPAddress ^> ^ ipAddresses, int port);
public void Connect (System.Net.IPAddress[] ipAddresses, int port);
member this.Connect : System.Net.IPAddress[] * int -> unit
Public Sub Connect (ipAddresses As IPAddress(), port As Integer)

參數

ipAddresses
IPAddress[]

您想連接之主機的 IPAddress 陣列。The IPAddress array of the host to which you intend to connect.

port
Int32

您想連接的通訊埠編號。The port number to which you intend to connect.

例外狀況

ipAddresses 參數為 nullThe ipAddresses parameter is null.

連接埠號碼無效。The port number is not valid.

嘗試存取通訊端時發生錯誤。An error occurred when attempting to access the socket.

Socket 已關閉。The Socket has been closed.

在呼叫堆疊中位置較高的呼叫端對於要求的作業沒有權限。A caller higher in the call stack does not have permission for the requested operation.

這個方法對使用 InterNetwork 旗標或 InterNetworkV6 旗標的通訊端有效。This method is valid for sockets that use the InterNetwork flag or the InterNetworkV6 flag.

範例

下列程式碼範例會使用 IP 位址和埠號碼來連接遠端主機。The following code example uses an IP Address and port number to connect with a remote host.

static void DoConnect( String^ host, int port )
{
   // Connect to the specified host.
   TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
   array<IPAddress^>^IPAddresses = Dns::GetHostAddresses( host );
   Console::WriteLine( "Establishing Connection to {0}", host );
   t->Connect( IPAddresses, port );
   Console::WriteLine( "Connection established" );
}
static void DoConnect(string host, int port)
{
    // Connect to the specified host.
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);
      
    IPAddress[] IPAddresses = Dns.GetHostAddresses(host);

    Console.WriteLine("Establishing connection to {0}", host);
    t.Connect(IPAddresses, port);

    Console.WriteLine("Connection established");
}

備註

這個方法通常會在呼叫BeginGetHostAddresses方法之後立即使用,這可以傳回單一主機的多個 IP 位址。This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host. 呼叫方法,以建立與專案陣列和埠號碼所指定之IPAddress主機的同步遠端主機連接。 ConnectCall the Connect method to establish a synchronous remote host connection to the host specified by the array of IPAddress elements and the port number. Connect方法將會封鎖,直到連接或失敗為止。The Connect method will block until it either connects or fails. 連接遠端主機之後,請使用GetStream方法來取得基礎。 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此NetworkStream來傳送和接收資料。Use this NetworkStream to send and receive data.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

另請參閱

Connect(String, Int32)

將用戶端連接到所指定主機上的指定連接埠。Connects the client to the specified port on the specified host.

public:
 void Connect(System::String ^ hostname, int port);
public void Connect (string hostname, int port);
member this.Connect : string * int -> unit
Public Sub Connect (hostname As String, port As Integer)

參數

hostname
String

您要連接之遠端主機的 DNS 名稱。The DNS name of the remote host to which you intend to connect.

port
Int32

您要連接之遠端主機的連接埠號碼。The port number of the remote host to which you intend to connect.

例外狀況

hostname 參數為 nullThe hostname parameter is null.

port 參數不是介於 MinPortMaxPortThe port parameter is not between MinPort and MaxPort.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列程式碼範例會使用主機名稱和埠號碼來連接遠端主機。The following code example uses the host name and port number to connect with a remote host.

//Uses a host name and port number to establish a socket connection.
TcpClient^ tcpClient = gcnew TcpClient;
tcpClient->Connect( "www.contoso.com", 11002 );

//Uses a host name and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);

'Uses a host name and port number to establish a socket connection.
Dim tcpClient As New TcpClient()

   tcpClient.Connect("www.contoso.com", 11002)

備註

呼叫這個方法,以建立與指定主機名稱和埠號碼的同步遠端主機連線。Call this method to establish a synchronous remote host connection to the specified host name and port number. Connect方法將會封鎖,直到連接或失敗為止。The Connect method will block until it either connects or fails. 連接遠端主機之後,請使用GetStream方法來取得基礎。 NetworkStreamAfter connecting with the remote host, use the GetStream method to obtain the underlying NetworkStream. 使用此NetworkStream來傳送和接收資料。Use this NetworkStream to send and receive data.

如果已啟用 ipv6,而且Connect(String, Int32)呼叫方法以連線至解析為 IPv6 和 IPv4 位址的主機,則會先嘗試連至 ipv6 位址的連線,再開始使用 IPv4 位址。If IPv6 is enabled and the Connect(String, Int32) method is called to connect to a host that resolves to both IPv6 and IPv4 addresses, the connection to the IPv6 address will be attempted first before the IPv4 address. 如果主機未接聽 IPv6 位址,這可能會影響延遲建立連線的時間。This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

另請參閱

適用於