Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
basic_socket_acceptor::async_accept (5 of 6 overloads)

Start an asynchronous accept.

    typename MoveAcceptHandler>
DEDUCED async_accept(
    endpoint_type & peer_endpoint,
    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.



An endpoint object into which the endpoint of the remote peer will be written. Ownership of the peer_endpoint object is retained by the caller, which must guarantee that it is valid until the handler is called.


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 boost::system::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 boost::asio::io_context::post().

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


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