System.Net.Sockets.Socket.BeginAccept Method

Begins an asynchronous operation to accept an incoming connection attempt.

Syntax

public IAsyncResult BeginAccept (AsyncCallback callback, object state)

Parameters

callback
The AsyncCallback delegate.
state
An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous System.Net.Sockets.Socket creation.

Exceptions

TypeReason
System.Net.Sockets.SocketException
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

Connection-oriented protocols can use the Socket.BeginAccept(AsyncCallback, object) method to asynchronously process incoming connection attempts. Accepting connections asynchronously gives you the ability to send and receive data within a separate execution thread. Before calling the Socket.BeginAccept(AsyncCallback, object) method, you must call the Socket.Listen(int) method to listen for and queue incoming connection requests.

You must create a callback method that implements the AsyncCallback delegate and pass its name to the Socket.BeginAccept(AsyncCallback, object) method. To do this, at the very minimum, you must pass the listening System.Net.Sockets.Socket object to Socket.BeginAccept(AsyncCallback, object) through the state parameter. If your callback needs more information, you can create a small class to hold the System.Net.Sockets.Socket and the other required information. Pass an instance of this class to the Socket.BeginAccept(AsyncCallback, object) method through the state parameter.

Your callback method should invoke the Socket.EndAccept(IAsyncResult) method. When your application calls Socket.BeginAccept(AsyncCallback, object), the system usually uses a separate thread to execute the specified callback method and blocks on Socket.EndAccept(IAsyncResult) until a pending connection is retrieved. Socket.EndAccept(IAsyncResult) will return a new System.Net.Sockets.Socket object that you can use to send and receive data with the remote host. You cannot use this returned System.Net.Sockets.Socket to accept any additional connections from the connection queue. If you want the original thread to block after you call the Socket.BeginAccept(AsyncCallback, object) method, use System.Threading.WaitHandle.WaitOne(int, bool). Call the Set method on a System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing.

The system may also use the calling thread to invoke the callback method. In this case, the IAsyncResult.CompletedSynchronously property on the returned IAsyncResult will be set to indicate that the Socket.BeginAccept(AsyncCallback, object) method completed synchronously.

For additional information on writing callback methods see [<topic://cpconcallbacksample>].

To cancel a pending call to the erload:System.Net.Sockets.Socket.BeginAccept method, close the System.Net.Sockets.Socket. When the Socket.Close method is called while an asynchronous operation is in progress, the callback provided to the erload:System.Net.Sockets.Socket.BeginAccept method is called. A subsequent call to the erload:System.Net.Sockets.Socket.EndAccept method will throw an ObjectDisposedException to indicate that the operation has been cancelled.

Note:

You can use the Socket.RemoteEndPoint property of the returned System.Net.Sockets.Socket to identify the remote host's network address and port number.

Note:

If you receive a System.Net.Sockets.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 MSDN for a detailed description of the error.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous System.Net.Sockets.Socket methods. After the first use of a particular context (a specific asynchronous System.Net.Sockets.Socket method, a specific System.Net.Sockets.Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

Example

The following excerpt from the System.Net.Sockets.Socket class overview example outlines an asynchronous accept operation.

C# Example

public class Server
{
  static void Main()
  {
    .
    .
    .
    listenSocket.BeginAccept(
      new AsyncCallback(Server.acceptCallback),
      listenSocket);
    .
    .
    .
    // EndAccept can be called here
    .
    .
    .
  }

  public static void
    acceptCallback(IAsyncResult asyncAccept)
  {
    Socket listenSocket =
      (Socket)asyncAccept.AsyncState;

    Socket serverSocket =
      listenSocket.EndAccept(asyncAccept);

    serverSocket.BeginReceive(...);
    .
    .
    .
  }
}

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0