Boost C++ 库

……全球最受尊敬、设计最精妙的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu,《C++ Coding Standards

connect (8 个重载中的第 2 个) - Boost C++ 函数库
PrevUpHomeNext

通过尝试序列中的每个端点来建立套接字连接。

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 成员函数来实现,每个终结点调用一次,直到成功建立连接。

参数

s

要连接的套接字。如果套接字已打开,它将被关闭。

endpoints

一系列端点。

ec

用于指示发生了什么错误(如果有)。如果序列为空,则设置为 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.
}

PrevUpHomeNext