Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Socket.EndAccept Method (IAsyncResult)

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

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

public Socket EndAccept (
	IAsyncResult asyncResult
)
public Socket EndAccept (
	IAsyncResult asyncResult
)
public function EndAccept (
	asyncResult : IAsyncResult
) : Socket
Not applicable.

Parameters

asyncResult

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 typeCondition

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.

InvalidOperationException

EndAccept method was previously called.

NotSupportedException

Windows NT is required for this method.

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.

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 an asynchronous request and creates a new Socket to accept an incoming connection request. For a complete example that demonstrates asynchronous communications with sockets, see Socket Code Examples.

public static void Listen_Callback(IAsyncResult ar){
     allDone.Set();
     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);	
}

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

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.