设置升压插座超时 [英] Set timeout for boost socket.connect

查看:102
本文介绍了设置升压插座超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在tcp::socket上使用boost::asio::connect.当一切正常时,connect立即返回,但在网络较差的情况下,connect在经过15秒的日志等待后超时.我不能等那么久,所以想减少超时时间.不幸的是,到目前为止,我还没有遇到任何解决方案.

I am using boost::asio::connect on a tcp::socket. When all goes fine, the connect returns immediately but on a poor network, the connect times out after a log wait of 15 seconds. I cannot afford to wait that long and so want to reduce the timeout. Unfortunately I have not come across any solution so far.

我看到了使用async_wait和decimate_timer一起使用的解决方案,但是所有这些示例都是用于接收/发送操作,而不是用于连接.

I see solutions where async_wait is been used together with deadline_timer but all those examples are for receive / send operations and not for connect.

有人可以帮我提供boost::asio::connect(socket, endpoints);的示例代码吗?要求是它应该在5秒而不是15秒后超时.

Can anyone help me with a sample code for boost::asio::connect(socket, endpoints);. Requirement is that it should timeout in 5 seconds instead of 15.

推荐答案

您是否看过以下示例?它包含一个带有超时的async_connect的示例代码.

Have you take a look to the following example? it contains a sample code an async_connect with timeout.

http://www. boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/timeouts/blocking_tcp_client.cpp

可以使用以下代码实现超时连接方法:

The connect with timeout method could be implemented using the following code:

void connect(const std::string& host, const std::string& service,
  boost::posix_time::time_duration timeout)  {
// Resolve the host name and service to a list of endpoints.
tcp::resolver::query query(host, service);
tcp::resolver::iterator iter = tcp::resolver(io_service_).resolve(query);

// Set a deadline for the asynchronous operation. As a host name may
// resolve to multiple endpoints, this function uses the composed operation
// async_connect. The deadline applies to the entire operation, rather than
// individual connection attempts.
deadline_.expires_from_now(timeout);

// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
// operation is incomplete. Asio guarantees that its asynchronous
// operations will never fail with would_block, so any other value in
// ec indicates completion.
boost::system::error_code ec = boost::asio::error::would_block;

// Start the asynchronous operation itself. The boost::lambda function
// object is used as a callback and will update the ec variable when the
// operation completes. The blocking_udp_client.cpp example shows how you
// can use boost::bind rather than boost::lambda.
boost::asio::async_connect(socket_, iter, var(ec) = _1);

// Block until the asynchronous operation has completed.
do io_service_.run_one(); while (ec == boost::asio::error::would_block);

// Determine whether a connection was successfully established. The
// deadline actor may have had a chance to run and close our socket, even
// though the connect operation notionally succeeded. Therefore we must
// check whether the socket is still open before deciding if we succeeded
// or failed.
if (ec || !socket_.is_open())
  throw boost::system::system_error(
      ec ? ec : boost::asio::error::operation_aborted);
}

这篇关于设置升压插座超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆