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

Definition

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)
Parameters
asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

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

Exceptions

An error occurred when attempting to access the socket.

Examples

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

Remarks

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.

Note

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 in the MSDN library for a detailed description of the error.

Note

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