通过尝试序列中的每个端点来建立套接字连接。
template< typename Protocol, typename Executor, typename EndpointSequence> Protocol::endpoint connect( basic_socket< Protocol, Executor > & s, const EndpointSequence & endpoints, boost::system::error_code & ec, constraint_t< is_endpoint_sequence< EndpointSequence >::value > = 0);
此函数尝试将套接字连接到一系列终结点中的一个。它通过重复调用套接字的 connect
成员函数来实现,每个终结点调用一次,直到成功建立连接。
要连接的套接字。如果套接字已打开,它将被关闭。
一系列端点。
用于指示发生了什么错误(如果有)。如果序列为空,则设置为 boost::asio::error::not_found
。否则,包含最后一次连接尝试的错误。
成功时,返回成功连接的端点。否则,返回默认构造的端点。
tcp::resolver r(my_context); tcp::resolver::query q("host", "service"); tcp::socket s(my_context); boost::system::error_code ec; boost::asio::connect(s, r.resolve(q), ec); if (ec) { // An error occurred. }