TcpClient.BeginConnect 方法

定義

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection.

多載

BeginConnect(IPAddress, Int32, AsyncCallback, Object)

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection. 遠端主機是由 IPAddress 和通訊埠編號 (Int32) 指定。The remote host is specified by an IPAddress and a port number (Int32).

BeginConnect(IPAddress[], Int32, AsyncCallback, Object)

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection. 遠端主機是由 IPAddress 陣列和通訊埠編號 (Int32) 指定。The remote host is specified by an IPAddress array and a port number (Int32).

BeginConnect(String, Int32, AsyncCallback, Object)

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection. 遠端主機是由主機名稱 (String) 和連接埠號碼 (Int32) 指定。The remote host is specified by a host name (String) and a port number (Int32).

BeginConnect(IPAddress, Int32, AsyncCallback, Object)

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection. 遠端主機是由 IPAddress 和通訊埠編號 (Int32) 指定。The remote host is specified by an IPAddress and a port number (Int32).

public:
 IAsyncResult ^ BeginConnect(System::Net::IPAddress ^ address, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginConnect (System.Net.IPAddress address, int port, AsyncCallback requestCallback, object state);
member this.BeginConnect : System.Net.IPAddress * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginConnect (address As IPAddress, port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

address
IPAddress

遠端主機的 IPAddressThe IPAddress of the remote host.

port
Int32

遠端主機的連接埠號碼。The port number of the remote host.

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object

包含連線作業資訊的使用者定義物件。A user-defined object that contains information about the connect operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

參考非同步連接的 IAsyncResult 物件。An IAsyncResult object that references the asynchronous connection.

例外狀況

address 參數為 nullThe address parameter is null.

嘗試存取通訊端時發生錯誤。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.

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

範例

下列程式碼範例會建立TcpClient並連接至遠端主機。The following code example creates a TcpClient and connects to a remote host.

   static void DoBeginConnect1( String^ host, int port )
   {
      // Connect asynchronously to the specifed host.
      TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
//      IPAddress^ remoteHost = gcnew IPAddress( host );
      array<IPAddress^>^ remoteHost = Dns::GetHostAddresses( host );
      connectDone->Reset();
      Console::WriteLine( "Establishing Connection to {0}", host );
      t->BeginConnect( remoteHost, port, gcnew AsyncCallback( &ConnectCallback ), t );

      // Wait here until the callback processes the connection.
      connectDone->WaitOne();
      Console::WriteLine( "Connection established" );
   }
        public static void DoBeginConnect1(string host, int port)
        {
            // Connect asynchronously to the specifed host.
            TcpClient t = new TcpClient(AddressFamily.InterNetwork);
//            IPAddress remoteHost = new IPAddress(host);
            IPAddress[] remoteHost = Dns.GetHostAddresses(host);

            connectDone.Reset();

            Console.WriteLine("Establishing Connection to {0}", 
                remoteHost[0]);
            t.BeginConnect(remoteHost[0], port, 
                new AsyncCallback(ConnectCallback), t);

            // Wait here until the callback processes the connection.
            connectDone.WaitOne();

            Console.WriteLine("Connection established");
        }

備註

非同步BeginConnect操作必須藉由EndConnect呼叫方法來完成。The asynchronous BeginConnect operation must be completed by calling the EndConnect method. 通常,方法是由asyncCallback委派叫用。Typically, the method is invoked by the asyncCallback delegate.

在作業完成之前,這個方法不會封鎖。This method does not block until the operation completes. 若要封鎖直到作業完成,請使用其中一個Connect方法多載。To block until the operation completes, use one of the Connect method overloads.

如需使用非同步程式設計模型的詳細資訊,請參閱以非同步方式呼叫同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

BeginConnect(IPAddress[], Int32, AsyncCallback, Object)

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection. 遠端主機是由 IPAddress 陣列和通訊埠編號 (Int32) 指定。The remote host is specified by an IPAddress array and a port number (Int32).

public:
 IAsyncResult ^ BeginConnect(cli::array <System::Net::IPAddress ^> ^ addresses, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginConnect (System.Net.IPAddress[] addresses, int port, AsyncCallback requestCallback, object state);
member this.BeginConnect : System.Net.IPAddress[] * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginConnect (addresses As IPAddress(), port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

addresses
IPAddress[]

至少一個指定遠端主機的 IPAddressAt least one IPAddress that designates the remote hosts.

port
Int32

遠端主機的通訊埠編號。The port number of the remote hosts.

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object

包含連線作業資訊的使用者定義物件。A user-defined object that contains information about the connect operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

參考非同步連接的 IAsyncResult 物件。An IAsyncResult object that references the asynchronous connection.

例外狀況

addresses 參數為 nullThe addresses parameter is null.

嘗試存取通訊端時發生錯誤。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.

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

範例

下列程式碼範例會建立TcpClient並連接至遠端主機。The following code example creates a TcpClient and connects to a remote host.

// Connect asynchronously to the specifed host.
static void DoBeginConnect2( String^ host, int port )
{
   TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
   array<IPAddress^>^remoteHost = Dns::GetHostAddresses( host );
   connectDone->Reset();
   Console::WriteLine( "Establishing Connection to {0}", host );
   t->BeginConnect( remoteHost, port, gcnew AsyncCallback(  &ConnectCallback ), t );

   // Wait here until the callback processes the connection.
   connectDone->WaitOne();
   Console::WriteLine( "Connection established" );
}
// Connect asynchronously to the specifed host.
public static void DoBeginConnect2(string host, int port)
{
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);
    IPAddress[] remoteHost = Dns.GetHostAddresses(host);

    connectDone.Reset();

    Console.WriteLine("Establishing Connection to {0}", host);
    t.BeginConnect(remoteHost, port, 
        new AsyncCallback(ConnectCallback), t);

    // Wait here until the callback processes the connection.
    connectDone.WaitOne();

    Console.WriteLine("Connection established");
}

備註

非同步BeginConnect操作必須藉由EndConnect呼叫方法來完成。The asynchronous BeginConnect operation must be completed by calling the EndConnect method. 通常,方法是由asyncCallback委派叫用。Typically, the method is invoked by the asyncCallback delegate.

在作業完成之前,這個方法不會封鎖。This method does not block until the operation completes. 若要封鎖直到作業完成,請使用其中一個Connect方法多載。To block until the operation completes, use one of the Connect method overloads.

如需使用非同步程式設計模型的詳細資訊,請參閱以非同步方式呼叫同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

這個方法通常會在呼叫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.

BeginConnect(String, Int32, AsyncCallback, Object)

開始遠端主機連接的非同步要求。Begins an asynchronous request for a remote host connection. 遠端主機是由主機名稱 (String) 和連接埠號碼 (Int32) 指定。The remote host is specified by a host name (String) and a port number (Int32).

public:
 IAsyncResult ^ BeginConnect(System::String ^ host, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginConnect (string host, int port, AsyncCallback requestCallback, object state);
member this.BeginConnect : string * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginConnect (host As String, port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

host
String

遠端主機的名稱。The name of the remote host.

port
Int32

遠端主機的連接埠號碼。The port number of the remote host.

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object

包含連線作業資訊的使用者定義物件。A user-defined object that contains information about the connect operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

參考非同步連接的 IAsyncResult 物件。An IAsyncResult object that references the asynchronous connection.

例外狀況

host 參數為 nullThe host parameter is null.

嘗試存取通訊端時發生錯誤。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.

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

範例

下列程式碼範例會建立TcpClient並連接至遠端主機。The following code example creates a TcpClient and connects to a remote host.

// Connect asynchronously to the specifed host.
static void DoBeginConnect3( String^ host, int port )
{
   TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
   connectDone->Reset();
   Console::WriteLine( "Establishing Connection to {0}", host );
   t->BeginConnect( host, port, gcnew AsyncCallback(  &ConnectCallback ), t );
   
   // Wait here until the callback processes the connection.
   connectDone->WaitOne();
   Console::WriteLine( "Connection established" );
}
// Connect asynchronously to the specifed host.
public static void DoBeginConnect3(string host, int port)
{
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);

    connectDone.Reset();

    Console.WriteLine("Establishing Connection to {0}", 
        host);
    t.BeginConnect(host, port, 
        new AsyncCallback(ConnectCallback), t);

    // Wait here until the callback processes the connection.
    connectDone.WaitOne();

    Console.WriteLine("Connection established");
}

備註

非同步BeginConnect操作必須藉由EndConnect呼叫方法來完成。The asynchronous BeginConnect operation must be completed by calling the EndConnect method. 通常,方法是由asyncCallback委派叫用。Typically, the method is invoked by the asyncCallback delegate.

在作業完成之前,這個方法不會封鎖。This method does not block until the operation completes. 若要封鎖直到作業完成,請使用其中一個Connect方法多載。To block until the operation completes, use one of the Connect method overloads.

如需使用非同步程式設計模型的詳細資訊,請參閱以非同步方式呼叫同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

適用於