I am writing a client-server program in c++ in linux. I want a functionality in my server that when server is waiting for some response from client, it should not wait indefinitely. But if no response is received say within 30 secs from client, it should disconnect the client. Is there any inbuilt function. Please help.
+1
A:
The select()
function lets you wait for an event from one of a set of given sockets. It also has a timeout value, so it will return if no event happens within that time.
Greg Hewgill
2010-08-08 05:17:29
+1
A:
Or, if you don't want to redesign your entire server, have a look at setsockopt() with the SO_TIMEOUT option. Doesn't work on all platforms, including some surprising ones, in which you have to use select().
EJP
2010-08-08 05:22:17
One can also call `select()` with a single socket handle immediately before a `recv()` to detect a timeout. Processes can have more than one pending call to `select()`.
Greg Hewgill
2010-08-08 06:09:44