asio C++ library

PrevUpHomeNext

basic_socket::async_connect

Start an asynchronous connect.

template<
    typename ConnectToken = default_completion_token_t<executor_type>>
DEDUCED async_connect(
    const endpoint_type & peer_endpoint,
    ConnectToken && token = default_completion_token_t< executor_type >());

This function is used to asynchronously connect a socket to the specified remote endpoint. It is an initiating function for an asynchronous operation, and 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.

Parameters

peer_endpoint

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

token

The completion token that will be used to produce a completion handler, which will be called when the connect 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(
  const asio::error_code& error // Result of operation.
);

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)
Example
void connect_handler(const asio::error_code& error)
{
  if (!error)
  {
    // Connect succeeded.
  }
}

...

asio::ip::tcp::socket socket(my_context);
asio::ip::tcp::endpoint endpoint(
    asio::ip::address::from_string("1.2.3.4"), 12345);
socket.async_connect(endpoint, connect_handler);
Per-Operation Cancellation

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


PrevUpHomeNext