Reputation: 41
I am using boost::asio::tcp::socket
to connect to a Server, and call
read_some()
to receive binary data from Server, the following are codes:
len = sock->read_some(boost::asio::buffer(pBuf), error);
if (error == boost::asio::error::eof) {
break;
}
else if (error) {
break;
}
But after the Server closed the socket, the client still blocked in read_some method, fail to detect disconnection with server and get the error message. Why this problem occurs?
Upvotes: 1
Views: 720
Reputation: 182769
It's either because you failed to correctly implement the protocol you are using on top of TCP or because of a design flaw in that protocol. If the protocol says to wait forever for data from the server without sending anything, then the fault is in the protocol. If the protocol says to timeout the read or to send some data and you're not doing that, then the flaw is in your implementation of the protocol.
TCP does not guarantee that a connection loss can be detected by a side that is not trying to send any data. Protocols that use TCP must be designed around this.
Upvotes: 2