简体   繁体   English

Boost ASIO服务器-检测空闲连接

[英]Boost ASIO server - detecting idle connection

The intention of this post is to figure out how to implement a Keep-Alive timeout on a Boost ASIO based HTTP server. 这篇文章的目的是弄清楚如何在基于Boost ASIO的HTTP服务器上实现Keep-Alive超时。

There are 2 parts to this - 有两个部分-

  1. When the client has closed the connection 客户端关闭连接后
  2. When the client connection is inactive 当客户端连接处于非活动状态时

I believe, 1) above can be detected by setting the TCP_KEEPIDLE, TCP_KEEPCNT and TCP_KEEPINTVL options on the native socket handle. 我相信,可以通过在本机套接字句柄上设置TCP_KEEPIDLE,TCP_KEEPCNT和TCP_KEEPINTVL选项来检测上述1)。

What is the best way to detect 2) above? 检测上述2)的最佳方法是什么?

I have tried setting the SO_RCVTIMEO and SO_SNDTIMEO on the native socket, but the server doesn't seem to close the connection at the end of the configured timeout interval. 我尝试在本机套接字上设置SO_RCVTIMEO和SO_SNDTIMEO,但是服务器似乎没有在配置的超时间隔结束时关闭连接。

Any pointers are much appreciated. 非常感谢任何指针。

Thanks! 谢谢!

You want to detect the case when the client has not sent you anything for X amount of time. 您要检测客户端在X时间内未向您发送任何内容的情况。 You can do this using a timer in Asio: How do I make the boost/asio library repeat a timer? 您可以使用Asio中的计时器来执行此操作: 如何使boost / asio库重复计时器?

Either construct one timer object per client, or construct one global timer and check the last message received time for all clients every time the timer fires. 每个客户端可以构造一个计时器对象,或者构造一个全局计时器,并在每次计时器触发时检查所有客户端的最后一条消息接收时间。

SO_RCVTIMEO won't help you, because when it expires the receive call returns EAGAIN or similar, and Asio will probably not do anything with this. SO_RCVTIMEO不会为您提供帮助,因为当过期时,接收调用将返回EAGAIN或类似消息,而Asio可能对此不做任何事情。 Read about that here: SO_RCVTIME and SO_RCVTIMEO not affecting Boost.Asio operations 在此处阅读有关内容: SO_RCVTIME和SO_RCVTIMEO不影响Boost.Asio操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM