asio C++ library

PrevUpHomeNext
basic_socket_acceptor::async_accept (4 of 6 overloads)

Start an asynchronous accept.

template<
    typename MoveAcceptHandler>
DEDUCED async_accept(
    asio::io_context & io_context,
    MoveAcceptHandler && handler);

This function is used to asynchronously accept a new connection. The function call always returns immediately.

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

Parameters

io_context

The io_context object to be used for the newly accepted socket.

handler

The handler to be called when the accept operation completes. Copies will be made of the handler as required. The function signature of the handler must be:

void handler(
  const asio::error_code& error, // Result of operation.
  typename Protocol::socket peer // On success, the newly accepted socket.
);

Regardless of whether the asynchronous operation completes immediately or not, the handler will not be invoked from within this function. Invocation of the handler will be performed in a manner equivalent to using asio::io_context::post().

Example
void accept_handler(const asio::error_code& error,
    asio::ip::tcp::socket peer)
{
  if (!error)
  {
    // Accept succeeded.
  }
}

...

asio::ip::tcp::acceptor acceptor(io_context);
...
acceptor.async_accept(io_context2, accept_handler);

PrevUpHomeNext