Edit

Share via


Socket.AcceptAsync Method

Definition

Overloads

AcceptAsync()

Accepts an incoming connection.

AcceptAsync(Socket)

Accepts an incoming connection.

AcceptAsync(SocketAsyncEventArgs)

Begins an asynchronous operation to accept an incoming connection attempt.

AcceptAsync(CancellationToken)

Accepts an incoming connection.

AcceptAsync(Socket, CancellationToken)

Accepts an incoming connection.

AcceptAsync()

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Accepts an incoming connection.

public:
 System::Threading::Tasks::Task<System::Net::Sockets::Socket ^> ^ AcceptAsync();
public System.Threading.Tasks.Task<System.Net.Sockets.Socket> AcceptAsync ();
member this.AcceptAsync : unit -> System.Threading.Tasks.Task<System.Net.Sockets.Socket>
Public Function AcceptAsync () As Task(Of Socket)

Returns

An asynchronous task that completes with the accepted Socket.

Exceptions

An invalid operation was requested. This exception occurs if the accepting Socket is not listening for connections or the accepted socket is bound.

You must call the Bind(EndPoint) and Listen(Int32) method before calling the AcceptAsync(SocketAsyncEventArgs) method.

This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified e parameter.

An error occurred when attempting to access the socket.

The Socket has been closed.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Accept().

Applies to

AcceptAsync(Socket)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Accepts an incoming connection.

public:
 System::Threading::Tasks::Task<System::Net::Sockets::Socket ^> ^ AcceptAsync(System::Net::Sockets::Socket ^ acceptSocket);
public System.Threading.Tasks.Task<System.Net.Sockets.Socket> AcceptAsync (System.Net.Sockets.Socket? acceptSocket);
member this.AcceptAsync : System.Net.Sockets.Socket -> System.Threading.Tasks.Task<System.Net.Sockets.Socket>
Public Function AcceptAsync (acceptSocket As Socket) As Task(Of Socket)

Parameters

acceptSocket
Socket

The socket to use for accepting the connection.

Returns

An asynchronous task that completes with the accepted Socket.

Exceptions

An invalid operation was requested. This exception occurs if the accepting Socket is not listening for connections or the accepted socket is bound.

You must call the Bind(EndPoint) and Listen(Int32) method before calling the AcceptAsync(SocketAsyncEventArgs) method.

This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified e parameter.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

AcceptAsync(SocketAsyncEventArgs)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Begins an asynchronous operation to accept an incoming connection attempt.

public:
 bool AcceptAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool AcceptAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.AcceptAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function AcceptAsync (e As SocketAsyncEventArgs) As Boolean

Parameters

e
SocketAsyncEventArgs

The SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

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

false if the I/O operation completed synchronously. The 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.

Exceptions

An argument is not valid. This exception occurs if the buffer provided is not large enough. The buffer must be at least 2 * (sizeof(SOCKADDR_STORAGE + 16) bytes.

This exception also occurs if multiple buffers are specified, the BufferList property is not null.

An argument is out of range. The exception occurs if the Count is less than 0.

An invalid operation was requested. This exception occurs if the accepting Socket is not listening for connections or the accepted socket is bound.

You must call the Bind(EndPoint) and Listen(Int32) method before calling the AcceptAsync(SocketAsyncEventArgs) method.

This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified e parameter.

An error occurred when attempting to access the socket.

The Socket has been closed.

Remarks

Connection-oriented protocols can use the AcceptAsync 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 AcceptAsync method, you must call the Listen 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 Socket to use for the incoming connection by specifying the Socket to use with the SocketAsyncEventArgs.AcceptSocket property.

If the SocketAsyncEventArgs.AcceptSocket property is null, a new Socket is constructed with the same AddressFamily, SocketType, and ProtocolType as the current 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 AcceptAsync 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 ConnectAsync 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 SocketAsyncEventArgs.SetBuffer method. Part of the buffer passed in will be consumed internally for use by 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 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 AcceptAsync 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 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 AcceptAsync method should be prepared to handle this condition.

See also

Applies to

AcceptAsync(CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Accepts an incoming connection.

public:
 System::Threading::Tasks::ValueTask<System::Net::Sockets::Socket ^> AcceptAsync(System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask<System.Net.Sockets.Socket> AcceptAsync (System.Threading.CancellationToken cancellationToken);
member this.AcceptAsync : System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<System.Net.Sockets.Socket>
Public Function AcceptAsync (cancellationToken As CancellationToken) As ValueTask(Of Socket)

Parameters

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation.

Returns

An asynchronous task that completes with the accepted Socket.

Exceptions

An invalid operation was requested. This exception occurs if the accepting Socket is not listening for connections or the accepted socket is bound.

You must call the Bind(EndPoint) and Listen(Int32) method before calling the AcceptAsync(SocketAsyncEventArgs) method.

This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified e parameter.

An error occurred when attempting to access the socket.

The Socket has been closed.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Accept().

Applies to

AcceptAsync(Socket, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Accepts an incoming connection.

public:
 System::Threading::Tasks::ValueTask<System::Net::Sockets::Socket ^> AcceptAsync(System::Net::Sockets::Socket ^ acceptSocket, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask<System.Net.Sockets.Socket> AcceptAsync (System.Net.Sockets.Socket? acceptSocket, System.Threading.CancellationToken cancellationToken);
member this.AcceptAsync : System.Net.Sockets.Socket * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<System.Net.Sockets.Socket>
Public Function AcceptAsync (acceptSocket As Socket, cancellationToken As CancellationToken) As ValueTask(Of Socket)

Parameters

acceptSocket
Socket

The socket to use for accepting the connection.

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation.

Returns

An asynchronous task that completes with the accepted Socket.

Exceptions

An invalid operation was requested. This exception occurs if the accepting Socket is not listening for connections or the accepted socket is bound.

You must call the Bind(EndPoint) and Listen(Int32) method before calling the AcceptAsync(SocketAsyncEventArgs) method.

This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified e parameter.

An error occurred when attempting to access the socket.

The Socket has been closed.

The cancellation token was canceled. This exception is stored into the returned task.

Applies to