asio C++ library

PrevUpHomeNext
basic_socket_acceptor::async_accept (4 of 8 overloads)

Start an asynchronous accept.

template<
    typename Executor1,
    typename MoveAcceptToken = DEFAULT>
DEDUCED async_accept(
    const Executor1 & ex,
    MoveAcceptToken && token = DEFAULT,
    typename constraint< is_executor< Executor1 >::value||execution::is_executor< Executor1 >::value >::type  = 0);

This function is used to asynchronously accept a new connection. It is an initiating function for an asynchronous operation, and always returns immediately.

This overload requires that the Protocol template parameter satisfy the AcceptableProtocol type requirements.

Parameters

ex

The I/O executor object to be used for the newly accepted socket.

token

The completion token that will be used to produce a completion handler, which will be called when the accept completes. Potential completion tokens include use_future, use_awaitable, yield_context, or a function object with the correct completion signature. The function signature of the completion handler must be:

void handler(
  // Result of operation.
  const asio::error_code& error,

  // On success, the newly accepted socket.
  typename Protocol::socket::template rebind_executor<
    Executor1>::other peer
);

Regardless of whether the asynchronous operation completes immediately or not, the completion handler will not be invoked from within this function. On immediate completion, invocation of the handler will be performed in a manner equivalent to using post.

Completion Signature
void(asio::error_code,
   typename Protocol::socket::template rebind_executor<
     Executor1>::other))
Example
void accept_handler(const asio::error_code& error,
    asio::ip::tcp::socket peer)
{
  if (!error)
  {
    // Accept succeeded.
  }
}

...

asio::ip::tcp::acceptor acceptor(my_context);
...
acceptor.async_accept(my_context2, accept_handler);
Per-Operation Cancellation

On POSIX or Windows operating systems, this asynchronous operation supports cancellation for the following cancellation_type values:


PrevUpHomeNext