Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boost::asio tcp socket shutdown blocking or not?

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?

like image 848
John Crane Avatar asked Nov 04 '11 19:11

John Crane


1 Answers

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..)

like image 132
Imran.Fanaswala Avatar answered Oct 16 '22 05:10

Imran.Fanaswala