Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] Socket closed notification?
From: Jonathan Franklin (franklin.jonathan_at_[hidden])
Date: 2009-12-15 12:08:45


On Mon, Dec 14, 2009 at 10:07 PM, Scott Gifford
<sgifford_at_[hidden]> wrote:
> Jonathan Franklin <franklin.jonathan_at_[hidden]> writes:
>> If the remote app closes unexpectedly, you read will return EOF.
>
> >From when I tested this a while back, my notes indicate I got one of
> these errors for an unclean shutdown (timeout or a TCP RST packet):

On a read, ASIO will immediately give you an EOF.

On a write, you will eventually get an error code that indicates the
remote socket has closed. Something along the lines of:
"An established connection was aborted by the software in your host machine"

>> Yeah, it's not reliable, and can take *many* writes.
>
> The TCP stack may have to do many writes, but the app should only have
> to write once and do a flush to detect an unresponsive other side.

The application *may* have to do many writes. Your mileage will vary,
depending on many variables, such as whether the remote host is on the
same network, etc.

IME, if the remote process lives on the same box, a single application
call to write() will most likely immediately detect the downed host.
If the remote process lives on another box on the same network, then
it will most likely take at least *two* application writes to detect,
even when using TCP keep-alives.

Note that if I can't always detect a downed host with a single call
(e.g. to write()), then I don't consider it a reliable method.

> At the very least, this is a technique I have consistently seen
> recommended, and has always worked for me.

The only reliable way to detect the remote host dropping off, is to
attempt a read. This is the consistent recommendation by socket
programming experts. Please refer to _Effective TCP/IP Programming_,
Tip 19; and Stevens.

I have attached some toy applications that you can play with.
Experiment with running the server on the local host, another box on
the same network, and if possible, out on the internet.

Jon






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