user1474341
user1474341

Reputation: 155

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.

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!

Upvotes: 2

Views: 896

Answers (1)

John Zwinck
John Zwinck

Reputation: 249183

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

Upvotes: 3

Related Questions