Socket.EndConnect(IAsyncResult) Method


Ends a pending asynchronous connection request.

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



An IAsyncResult that stores state information and any user defined data for this asynchronous operation.


asyncResult is null.

asyncResult was not returned by a call to the BeginConnect(EndPoint, AsyncCallback, Object) method.

EndConnect(IAsyncResult) was previously called for the asynchronous connection.

An error occurred when attempting to access the 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 is a blocking method that completes the asynchronous remote host connection request started in the BeginConnect method.

Before calling BeginConnect, you need to create a callback method that implements the AsyncCallback delegate. This callback method executes in a separate thread and is called by the system after BeginConnect returns. The callback method must accept the IAsyncResult returned by the BeginConnect method as a parameter.

Within the callback method, call the AsyncState method of the IAsyncResult parameter to obtain the Socket on which the connection attempt is being made. After obtaining the Socket, you can call the EndConnect method to successfully complete the connection attempt.


If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 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. For more information, see Network Tracing in the .NET Framework.

Applies to

See also