开发者

In case of a blocking recv call, if the peer side system reboots, the call doesn't come of out recv. Why?

When my code is in a blocking recv call, if the other side reboots, then this开发者_运维知识库 side recv call doesn't get to know about it and just goes into a hung state.

How to avoid this?


By default, if the other side of the connection disappears without terminating the connection properly, the OS on your side has no way of knowing that no further data will be coming. That's why recv() will block forever in this situation.

If you want to have a timeout, then set the socket to non-blocking and use select() to wait for it to become readable. select() allows you to specify a timeout.

Alternatively, you can set the SO_KEEPALIVE socket option with setsockopt(). This will enable the sending of TCP "keepalives", that will allow your side to detect a stale connection. (Do note that with the default settings, it can take a long time to detect that the connection has gone).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜