TcpClient.EndConnect(IAsyncResult) Method

Definition

Ends a pending asynchronous connection attempt.

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

Parameters

asyncResult
IAsyncResult

An IAsyncResult object returned by a call to BeginConnect.

Exceptions

The asyncResult parameter is null.

The asyncResult parameter was not returned by a call to a BeginConnect method.

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

An error occurred when attempting to access the Socket.

The underlying Socket has been closed.

Examples

The following code example ends the asynchronous connection attempt.

static ManualResetEvent^ connectDone = gcnew ManualResetEvent( false );
static void ConnectCallback( IAsyncResult^ ar )
{
   connectDone->Set();
   TcpClient^ t = safe_cast<TcpClient^>(ar->AsyncState);
   t->EndConnect( ar );
}
public static ManualResetEvent connectDone =
    new ManualResetEvent(false);

public static void ConnectCallback(IAsyncResult ar)
{
    connectDone.Set();
    TcpClient t = (TcpClient)ar.AsyncState;
    t.EndConnect(ar);
}

Remarks

This method blocks until the operation is complete. To perform this operation synchronously, use a Connect method.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Applies to