System.Net.Sockets.Socket.AcceptAsync Method

Begins an asynchronous operation to accept an incoming connection attempt.

Syntax

public bool AcceptAsync (SocketAsyncEventArgs e)

Parameters

e
The System.Net.Sockets.SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

Returns true if the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the e parameter will be raised upon completion of the operation.

Returns false if the I/O operation completed synchronously. The SocketAsyncEventArgs.Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

Remarks

Connection-oriented protocols can use the Socket.AcceptAsync(SocketAsyncEventArgs) 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.AcceptAsync(SocketAsyncEventArgs) method, you must call the Socket.Listen(int) method to listen for and queue incoming connection requests.

To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and hook it to the SocketAsyncEventArgs.Completed event.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required:

The caller can optionally specify an existing System.Net.Sockets.Socket to use for the incoming connection by specifying the System.Net.Sockets.Socket to use with the SocketAsyncEventArgs.AcceptSocket property.

If the SocketAsyncEventArgs.AcceptSocket property is null, a new System.Net.Sockets.Socket is constructed with the same Socket.AddressFamily, Socket.SocketType, and Socket.ProtocolType as the current System.Net.Sockets.Socket and set as the SocketAsyncEventArgs.AcceptSocket property.

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the Socket.AcceptAsync(SocketAsyncEventArgs) method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

Optionally, a buffer may be provided in which to receive the initial block of data on the socket after the Socket.ConnectAsync(SocketAsyncEventArgs) method succeeds. In this case, the SocketAsyncEventArgs.Buffer property needs to be set to the buffer containing the data to receive and the SocketAsyncEventArgs.Count property needs to be set to the maximum number of bytes of data to receive in the buffer. These properties can be set using the erload:System.Net.Sockets.SocketAsyncEventArgs.SetBuffer method. Part of the buffer passed in will be consumed internally for use by the the underlying Winsock AcceptEx call. This means that the amount of data returned will always be less than the value of the SocketAsyncEventArgs.Count property on the System.Net.Sockets.SocketAsyncEventArgs instance provided. The amount of the buffer used internally varies based on the address family of the socket. The minimum buffer size required is 288 bytes. If a larger buffer size is specified, then the System.Net.Sockets.Socket will expect some extra data other than the address data received by the Winsock AcceptEx call and will wait until this extra data is received. If a timeout occurs, the connection is reset. So if extra data is expected of a specific amount, then the buffer size should be set to the minimum buffer size plus this amount.

The completion callback method should examine the SocketAsyncEventArgs.SocketError property to determine if the Socket.AcceptAsync(SocketAsyncEventArgs) operation was successful.

The SocketAsyncEventArgs.Completed event can occur in some cases when no connection has been accepted and cause the SocketAsyncEventArgs.SocketError property to be set to SocketError.ConnectionReset. This can occur as a result of port scanning using a half-open SYN type scan (a SYN -> SYN-ACK -> RST sequence). Applications using the Socket.AcceptAsync(SocketAsyncEventArgs) method should be prepared to handle this condition.

Requirements

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