Socket.EndConnect Method (IAsyncResult)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Ends a pending asynchronous connection request.

Namespace:   System.Net.Sockets
Assembly:  System (in System.dll)

Public Sub EndConnect (
	asyncResult As IAsyncResult
)

Parameters

asyncResult
Type: System.IAsyncResult

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

Exception Condition
ArgumentNullException

asyncResult is null.

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.

System_CAPS_noteNote

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.

System_CAPS_noteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

The following code example ends the asynchronous connection attempt. For a complete example that demonstrates asynchronous communications with sockets, see Socket Code Examples.

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

.NET Framework
Available since 1.1
Return to top
Show: