By making a read operation complete, I take to mean making socket::read_some
return or having the handler passed to socket::async_read_some
be invoked. Note that I'm on the receiver-side of the socket and it is me who have initiated the read operation. What I want to accomplish is to actively close a socket connection. The connection may also be passively closed, which is indicated by the read operation complete with boost::asio::error::eof
.
I've tried socket::shutdown()
followed by a call to socket::close()
like the following:
boost::system::error_code err;
if (psocket->is_open()) {
psocket->shutdown(tcp::socket::shutdown_both, err);
psocket->close(err);
}
The problem with this approach is that if the connection is passively closed after the call to is_open()
returned true
, then on some OS like the Mac OS X, the following call to close()
will result in a segmentation fault by accessing an already nulled pointer. On the other hand, a single call to shutdown()
simply won't make the read operation complete. So, how can I actively make the read operation complete in a safe manner?