Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Socket.EndConnect Method

Ends a pending asynchronous connection request.

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

public void EndConnect (
	IAsyncResult asyncResult
)
public void EndConnect (
	IAsyncResult asyncResult
)
public function EndConnect (
	asyncResult : IAsyncResult
)

Parameters

asyncResult

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

Exception typeCondition

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.

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.

NoteNote

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

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

allDone.Set();
Socket s = (Socket)ar.get_AsyncState();
s.EndConnect(ar);
StateObject so2 = new StateObject();
so2.workSocket = s;
ubyte buff[] = Encoding.get_ASCII().GetBytes("This is a test");
s.BeginSend(buff, 0, buff.length, (SocketFlags)0,
    new AsyncCallback(Async_Send_Receive.Send_Callback), so2);

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft