user349146
user349146

Reputation: 59

Timeout in socket programming

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.

Upvotes: 1

Views: 454

Answers (2)

user207421
user207421

Reputation: 310850

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().

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992717

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.

Upvotes: 1

Related Questions