2

The intention of this post is to figure out how to implement a Keep-Alive timeout on a Boost ASIO based HTTP server.

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.

What is the best way to detect 2) above?

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.

Any pointers are much appreciated.

Thanks!

user1474341
  • 125
  • 1
  • 12
  • If the client closes its connection nicely, then your end of the connection will be marked as readable (if you use polling) and then `read` or `recv` will return `0`. – Some programmer dude Jul 10 '17 at 10:55

1 Answers1

3

You want to detect the case when the client has not sent you anything for X amount of time. You can do this using a timer in Asio: How do I make the boost/asio library repeat a timer?

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. Read about that here: SO_RCVTIME and SO_RCVTIMEO not affecting Boost.Asio operations

John Zwinck
  • 239,568
  • 38
  • 324
  • 436