asio C++ library



Start an asynchronous connect.

    typename ConnectHandler>
void-or-deduced async_connect(
    const endpoint_type & peer_endpoint,
    ConnectHandler handler);

This function is used to asynchronously connect a socket to the specified remote endpoint. The function call always returns immediately.

The socket is automatically opened if it is not already open. If the connect fails, and the socket was automatically opened, the socket is not returned to the closed state.



The remote endpoint to which the socket will be connected. Copies will be made of the endpoint object as required.


The handler to be called when the connection 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

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_service::post().

void connect_handler(const asio::error_code& error)
  if (!error)
    // Connect succeeded.


asio::ip::tcp::socket socket(io_service);
asio::ip::tcp::endpoint endpoint(
    asio::ip::address::from_string(""), 12345);
socket.async_connect(endpoint, connect_handler);