Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost ASIO - SO_LINGER Socket option is not working
From: Nick Aschberger (nick.aschberger_at_[hidden])
Date: 2009-07-06 23:47:03


Hi All,

I know it's been a long time since I visited this thread - thanks Zach and Igor for your input here.

For all interested folks, it turns out that neither shutdown() or close() will guarantee that the data is sent. The actual behaviour is implementation/OS specific.

I found a blog entry that discussed this here:
http://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable

In the end I have rearranged the code so that the final write is acked by the receiving side - now the communication is reliable.

cheers

Nick



Zachary Turner wrote:
On Thu, May 28, 2009 at 9:42 PM, Nick Aschberger <nick.aschberger@astc-design.com> wrote:

I experimented with m_socket->shutdown() instead of close, and it made little difference. In fact, from the documentation it seems shutdown() is the one I definitely don't want to call, as close() is supposed to wait for data to be sent.

This is a common point of confusion with sockets programming.  Here is how it works according to my understanding.  Note that I know nothing of how boost sockets are implemented, the following applies specifically to the BSD socket interface, which means that by extension it should apply to boost sockets although I can't guarantee that.

1) shutdown() initiates a graceful termination sequence.  It never blocks, regardless of the value of SO_LINGER.  shutdown() specifically tells the kernel "I'm done, start flushing out any pending data that's left over".  But it never frees any underlying O/S resources or handles.

2) close() terminates the connection and frees O/S handles.  On windows, the function is called closesocket() and first initiates a shutdown (according to the definition above), if one has not been initiated already.  This behavior is not guaranteed as far as I know (someone correct me if I'm wrong) and probably does not happen on O/Ses other than Windows.  Even the windows documentation recommends not relying on it.   *IF* a graceful shutdown has been initiated *AND* the SO_LINGER option is set, then close() blocks until either a) the linger timeout has expired, or b) all pending data has been sent, whichever comes first.

Therefore, I believe the correct sequence is to first set the SO_LINGER option, then call shutdown, then call close.

Have you tried that?

_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net