This documentation is archived and is not being maintained.

Socket.EndAccept Method

Asynchronously accepts an incoming connection attempt and creates a new Socket to handle remote host communication.

[Visual Basic]
Public Function EndAccept( _
   ByVal asyncResult As IAsyncResult _
) As Socket
public Socket EndAccept(
 IAsyncResult asyncResult
public: Socket* EndAccept(
 IAsyncResult* asyncResult
public function EndAccept(
   asyncResult : IAsyncResult
) : Socket;


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

Return Value

A Socket to handle communication with the remote host.


Exception Type Condition
ArgumentNullException asyncResult is a null reference (Nothing in Visual Basic).
ArgumentException asyncResult was not created by a call to BeginAccept.
SocketException An error occurred when attempting to access the socket. See the Remarks section for more information.
ObjectDisposedException The Socket has been closed.


EndAccept completes a call to BeginAccept. Before calling BeginAccept, 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 the BeginAccept method returns. It must accept the asyncResult parameter returned from the BeginAccept method.

Within the callback method, call the AsyncState method of the asyncResult parameter to obtain the Socket on which the connection attempt is being made. After obtaining the Socket, you can call the EndAccept method to successfully complete the connection attempt.The EndAccept method blocks until a connection is pending in the incoming connection queue. The EndAccept method accepts the incoming connection and returns a new Socket that can be used to send data to and receive data from the remote host.

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 an asynchronous request and creates a new Socket to accept an incoming connection request.

[Visual Basic] 
Dim s As Socket = CType(ar.AsyncState, Socket)
Dim s2 As Socket = s.EndAccept(ar)
Dim so2 As New StateObject()
so2.workSocket = s2
s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE, 0, New AsyncCallback(AddressOf Async_Send_Receive.Read_Callback), so2)
   End Sub 'Listen_Callback

Socket s = (Socket) ar.AsyncState;
Socket s2 = s.EndAccept(ar);
StateObject so2 = new StateObject();
so2.workSocket = s2;
s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0,
                      new AsyncCallback(Async_Send_Receive.Read_Callback), so2);    

Socket* s = __try_cast<Socket*>(ar->AsyncState);
Socket* s2 = s->EndAccept(ar);
StateObject* so2 = new StateObject();
so2->workSocket = s2;
s2->BeginReceive(so2->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None,
    new AsyncCallback(0, &Async_Send_Receive::Read_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 | BeginAccept | AsyncCallback | AsyncState