Socket.EndConnect(IAsyncResult) 方法


结束挂起的异步连接请求。Ends a pending asynchronous connection request.

 void EndConnect(IAsyncResult ^ result);
public void EndConnect (IAsyncResult result);
member this.EndConnect : IAsyncResult -> unit
Public Sub EndConnect (result As IAsyncResult)



IAsyncResult,它存储此异步操作的状态信息以及所有用户定义的数据。An IAsyncResult that stores state information and any user defined data for this asynchronous operation.


asyncResultnullasyncResult is null.

BeginConnect(EndPoint, AsyncCallback, Object) 方法的调用未返回 asyncResultasyncResult was not returned by a call to the BeginConnect(EndPoint, AsyncCallback, Object) method.

先前曾为异步连接调用过 EndConnect(IAsyncResult)EndConnect(IAsyncResult) was previously called for the asynchronous connection.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

Socket 已关闭。The Socket has been closed.


下面的代码示例结束了异步连接尝试。The following code example ends the asynchronous connection attempt. 有关演示与套接字的异步通信的完整示例,请参阅套接字代码示例For a complete example that demonstrates asynchronous communications with sockets, see Socket Code Examples.

Socket^ s = safe_cast<Socket^>(ar->AsyncState);
s->EndConnect( ar );
StateObject^ so2 = gcnew StateObject;
so2->workSocket = s;
array<Byte>^ buff = Encoding::ASCII->GetBytes( "This is a test" );
s->BeginSend( buff, 0, buff->Length, SocketFlags::None,
   gcnew AsyncCallback( &Async_Send_Receive::Send_Callback ), so2 );
Socket s = (Socket) ar.AsyncState;
StateObject so2 = new StateObject();
so2.workSocket = s;
byte[] buff = Encoding.ASCII.GetBytes("This is a test");
s.BeginSend(buff, 0, buff.Length,0,
                      new AsyncCallback(Async_Send_Receive.Send_Callback), so2);    
Public Shared Sub Connect_Callback(ar As IAsyncResult)

   Dim s As Socket = CType(ar.AsyncState, Socket)
   Dim so2 As New StateObject()
   so2.workSocket = s
   Dim buff As Byte() = Encoding.ASCII.GetBytes("This is a test")
   s.BeginSend(buff, 0, buff.Length, 0, New AsyncCallback(AddressOf Async_Send_Receive.Send_Callback), so2)
End Sub


EndConnect 是一种阻止方法,用于完成在 BeginConnect 方法中启动的异步远程主机连接请求。EndConnect is a blocking method that completes the asynchronous remote host connection request started in the BeginConnect method.

在调用 BeginConnect之前,需要创建实现 AsyncCallback 委托的回调方法。Before calling BeginConnect, you need to create a callback method that implements the AsyncCallback delegate. 此回调方法在单独的线程中执行,并在 BeginConnect 返回后由系统调用。This callback method executes in a separate thread and is called by the system after BeginConnect returns. 回调方法必须接受 BeginConnect 方法作为参数返回的 IAsyncResultThe callback method must accept the IAsyncResult returned by the BeginConnect method as a parameter.

在回调方法中,调用 IAsyncResult 参数的 AsyncState 方法,以获取在其上进行连接尝试的 SocketWithin the callback method, call the AsyncState method of the IAsyncResult parameter to obtain the Socket on which the connection attempt is being made. 获取 Socket后,可以调用 EndConnect 方法成功完成连接尝试。After obtaining the Socket, you can call the EndConnect method to successfully complete the connection attempt.


如果收到 SocketException,请使用 SocketException.ErrorCode 属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码文档,以获取有关错误的详细说明。After you have obtained this code, 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.