Socket.EndConnect(IAsyncResult) Socket.EndConnect(IAsyncResult) Socket.EndConnect(IAsyncResult) Socket.EndConnect(IAsyncResult) Method

定义

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

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

参数

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

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

异常

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

示例

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

allDone->Set();
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 );
allDone.Set();
Socket s = (Socket) ar.AsyncState;
s.EndConnect(ar);
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)
   

   allDone.Set()
   Dim s As Socket = CType(ar.AsyncState, Socket)
   s.EndConnect(ar)
   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 'Connect_Callback

注解

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. 回调方法必须接受IAsyncResult返回的BeginConnect方法作为参数。The callback method must accept the IAsyncResult returned by the BeginConnect method as a parameter.

在回调方法中,调用AsyncState方法IAsyncResult参数来获取Socket上可由连接尝试。Within 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.

适用于

另请参阅