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 >());
此函数用于将套接字异步连接到指定的远程端点。 它是 异步操作 的启动函数,并始终立即返回。
如果套接字尚未打开,则会自动打开。 如果连接失败,并且套接字是自动打开的,则套接字不会返回到关闭状态。
套接字将要连接到的远程端点。 将根据需要制作端点对象的副本。
完成令牌,用于生成完成处理程序,该处理程序将在连接完成时被调用。 潜在的完成令牌包括 use_future
、use_awaitable
、yield_context
或具有正确完成签名的函数对象。 完成处理程序的函数签名必须是
void handler( const boost::system::error_code& error // Result of operation. );
无论异步操作是否立即完成,完成处理程序都不会从此函数内部调用。 在立即完成时,处理程序的调用将以等同于使用 async_immediate
的方式执行。
void(boost::system::error_code)
void connect_handler(const boost::system::error_code& error) { if (!error) { // Connect succeeded. } } ... boost::asio::ip::tcp::socket socket(my_context); boost::asio::ip::tcp::endpoint endpoint( boost::asio::ip::address::from_string("1.2.3.4"), 12345); socket.async_connect(endpoint, connect_handler);
在 POSIX 或 Windows 操作系统上,此异步操作支持以下 cancellation_type
值的取消
cancellation_type::terminal
cancellation_type::partial
cancellation_type::total