This documentation is archived and is not being maintained.

Socket.EndConnect Method

Ends a pending asynchronous connection request.

[Visual Basic]
Public Sub EndConnect( _
   ByVal asyncResult As IAsyncResult _
public void EndConnect(
 IAsyncResult asyncResult
public: void EndConnect(
 IAsyncResult* asyncResult
public function EndConnect(
   asyncResult : IAsyncResult


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


Exception Type Condition
ArgumentNullException asyncResult is a null reference (Nothing in Visual Basic).
ArgumentException asyncResult was not returned by a call to the BeginConnect method.
InvalidOperationException EndConnect was previously called for the asynchronous connection.
SocketException An error occurred when attempting to access the socket. See the Remarks section for more information.
ObjectDisposedException The Socket has been closed.


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 SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.


[Visual Basic, C#, C++] The following example ends the asynchronous connection attempt.

[Visual Basic] 
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 'Connect_Callback

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);    

Socket* s = __try_cast<Socket*>(ar->AsyncState);
StateObject* so2 = new StateObject();
so2->workSocket = s;
Byte buff[] = Encoding::ASCII->GetBytes(S"This is a test");
s->BeginSend(buff, 0, buff->Length, SocketFlags::None,
    new AsyncCallback(0, &Async_Send_Receive::Send_Callback), so2);    

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | Including Asynchronous Calls | BeginConnect | AsyncCallback | AsyncState