I am writing a client-server program in c++ in linux. I want a functionality in my server that when server is waiting for som开发者_如何学Goe 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.
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.
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().
精彩评论