boost::asio TCP socket accept/read/write all provide async version, but not shutdown.
In my code, I just call socket.close(), and most of the time it works fine. It triggered a graceful TCP shutdown.
But some times, close() simply close the socket without TCP shutdown. As a result, I have to call shutdown() instead. But I don't want to block my code. Is shutdown() blocking in boost:asio? How about close()? Is close() blocking?
First of all, the shutdown()
and close()
calls in Boost.Asio call the underlying BSD socket implementation. So there is nothing "special" about Asio's shutdown() or close() calls.
shutdown()
doesn't block. It is typically used to disable sends/receives or both (ie, send an EOF to the other end). It will NOT destroy the socket (i.e. the socket resource is NOT freed)
close()
will free the socket resource. It may also block depending on the SO_LINGER
option. But SO_LINGER
is a tricky beast, to convince you:
http://lists.freebsd.org/pipermail/freebsd-questions/2004-June/049093.html and http://developerweb.net/viewtopic.php?id=2982.
However, if you are using non-blocking sockets (i.e. O_NONBLOCK, which is what Boost.Asio is really wrapped up around), then close() doesn't block.
Further reading:
http://linux.die.net/man/3/shutdown http://linux.die.net/man/3/close
And if you are Windows: http://msdn.microsoft.com/en-us/library/windows/desktop/ms738547%28v=vs.85%29.aspx (read the comments, apparently the documented graceful shutdown techniques don't work all the time..)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With