Reputation: 551
I'm writing a TCP server that uses non-blocking sockets and epoll() for I/O multiplexing. I want to detect idle connections so that I can close them. I cannot use setsockopt with RCVTIMEO
because sockets don't block. How can we set a timeout on a nonblocking socket?
This is related to another question of mine: Time out idle connections in epoll based server
If dobuble post please tell me and I will delete this or the other question.
Upvotes: 1
Views: 779
Reputation: 136266
Use a timer with your socket. The event loop library you use should provide timers.
Alternatively, you can have one global timer that fires, say, once per minute. In this timer callback you check the last activity time of each client connection and disconnect ones that hasn't been active for a while.
Upvotes: 2