提高asio超时

发布于 2024-11-07 18:34:05 字数 957 浏览 1 评论 0原文

可能的重复:
如何在 boost 中设置阻塞套接字的超时亚洲?

我之前读过一些关于超时的条目,但我不明白。

我想要一个定义的连接超时。 连接代码如下所示:

try{
  boost::asio::ip::tcp::resolver              resolver(m_ioService);
  boost::asio::ip::tcp::resolver::query       query(link.get_host(), link.get_scheme());
  boost::asio::ip::tcp::resolver::iterator    endpoint_iterator = resolver.resolve(query);
  boost::asio::ip::tcp::resolver::iterator    end;
  boost::system::error_code                   error   =   boost::asio::error::host_not_found;

  while (error && endpoint_iterator != end)
   {
    m_socket.close();
    m_socket.connect(*endpoint_iterator++, error);
   }
}

我也想要一个读取超时。

我使用 boost::asio::read_until(m_socket, response, "\r\n"); 来读取标头。

是否可以设置简单的超时?

Possible Duplicate:
How to set a timeout on blocking sockets in boost asio?

I read some of the entries before about the timeout but I don't understand.

I want a defined timeout for the connection.
the connect code looks like:

try{
  boost::asio::ip::tcp::resolver              resolver(m_ioService);
  boost::asio::ip::tcp::resolver::query       query(link.get_host(), link.get_scheme());
  boost::asio::ip::tcp::resolver::iterator    endpoint_iterator = resolver.resolve(query);
  boost::asio::ip::tcp::resolver::iterator    end;
  boost::system::error_code                   error   =   boost::asio::error::host_not_found;

  while (error && endpoint_iterator != end)
   {
    m_socket.close();
    m_socket.connect(*endpoint_iterator++, error);
   }
}

also I want a read timeout.

I use boost::asio::read_until(m_socket, response, "\r\n"); for read the header.

is it possible to set SIMPLE a timeout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-11-14 18:34:05

首先,我相信您应该始终使用异步方法,因为它们更好,并且您的设计只会从反应器模式方法中受益。
在糟糕的情况下,您很着急并且正在进行原型设计,同步方法可能会很有用。在这种情况下,我确实同意你的观点,如果没有任何超时支持,它们就无法在现实世界中使用。

我所做的非常简单:

void HttpClientImpl::configureSocketTimeouts(boost::asio::ip::tcp::socket& socket)
{
#if defined OS_WINDOWS
    int32_t timeout = 15000;
    setsockopt(socket.native(), SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
    setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
#else
    struct timeval tv;
    tv.tv_sec  = 15; 
    tv.tv_usec = 0;         
    setsockopt(socket.native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
    setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
#endif
}

根据 OS_WINDOWS 宏,上面的代码可以在 Windows、Linux 和 MAC OS 上运行。

Fist of all I believe that you should ALWAYS use the async methods since they are better and your design will only benefit from a reactor pattern approach.
In the bad case that you're in a hurry and you're kind of prototyping, the sync methods can be useful. In this case I do agree with you that without any timeout support, they cannot be used in the real world.

What I did was very simple:

void HttpClientImpl::configureSocketTimeouts(boost::asio::ip::tcp::socket& socket)
{
#if defined OS_WINDOWS
    int32_t timeout = 15000;
    setsockopt(socket.native(), SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
    setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
#else
    struct timeval tv;
    tv.tv_sec  = 15; 
    tv.tv_usec = 0;         
    setsockopt(socket.native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
    setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
#endif
}

The code above works both on windows and on Linux and on MAC OS, according to the OS_WINDOWS macro.

北音执念 2024-11-14 18:34:05

使用 boost::asio 和 read_until 等同步调用不允许轻松设置超时。

我建议转向异步调用(如async_read),并将其与deadline_timer 结合起来以实现此目标。

Using boost::asio and the sychronous calls like read_until do not allow for easily setting a timeout.

I'd suggest moving to asynchronous calls (like async_read), and combining that with a deadline_timer to accomplish this goal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文