user832096
user832096

Reputation: 373

Socet recv hangs when server crashes

I am working on client server program using sockets on Windows and MAC and currently facing a issue where client call recv blocks forever when server program crashes/killed using task manager. I send some data to server and asynchronously wait for reply using recv. This reply will come from server after some time which can not be determined. I was expecting an error from recv when other ends of socket dies. Not sure how to handle this situation.

Suggestions ?

Thanks,

Upvotes: 0

Views: 850

Answers (2)

Steffen Ullrich
Steffen Ullrich

Reputation: 123631

Your recv should not hang (but instead indicate a closed connection) if you are using TCP and the peer socket gets properly closed and if the communication between both systems is working (no broken cable etc). In UNIX the socket gets properly closed by the kernel even if the process crashes or gets killed, but I don't know about the behavior of Windows in this case.

recv will still hang if you are using UDP for communication, because there is no explicit connection close.

Upvotes: 0

user207421
user207421

Reputation: 311055

Set a read timeout with setsockopt() and the SO_RCVTIMEO option, setting it to some sensible value.

Upvotes: 1

Related Questions