Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] Socket closed notification?
From: Scott Gifford (sgifford_at_[hidden])
Date: 2009-12-14 23:17:33


Andrew Maclean <andrew.amaclean_at_[hidden]> writes:

> On Tue, Dec 15, 2009 at 2:50 PM, Scott Gifford
> <sgifford_at_[hidden]> wrote:
>> Jonathan Franklin <franklin.jonathan_at_[hidden]> writes:
>>
>>> On Mon, Dec 14, 2009 at 10:52 AM, Nicola Michael Gutberlet
>>> <nicola.gutberlet_at_[hidden]> wrote:
>>>> I’m using TCP sockets and wonder, if there’s a possibility to get an
>>>> automatic notification when one side of a socket is closed (even if the
>>>> other side is crashed / not closed properly).
>>>
>>> You generally have to do a read on the socket to reliably detect when
>>> the other side has closed (or crashed).
>>
>> And a write, too, or else use TCP keepalives.  Otherwise a crashed
>> client which is never sending anything will not send any error
>> indication for you to read.
>
> Do you have any ideas on how to do this within the ASIO framework?

Sure, just use any of the read functions and see if they return an
error. In my application, I use read_until(), but any function that
reads should do.

Jonathan Franklin <franklin.jonathan_at_[hidden]> writes:

[...]

> Unfortunately, write isn't reliable since it will always succeed until
> you fill up your send buffer.

Well, OK, maybe a write and a flush. Certainly there are othe
buffers, but typically a TCP implementation will time out at some
point if it has some pending data which is not acknowledged, and
typically once data is flushed to the TCP layer it will try to write
it and begin waiting for its timeout.

----Scott.


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