The are many circumstances with TCP connections that a server will not
get a notification that a client has disappeared, for example, if the
client crashes, has its power turned off or its network cable
disconnected. The only way the server has of knowing something is
wrong is to timeout and close the connection. As a consequence, if
you want a connection to stay open for an extended period of time
without any activity, at least one of the two parties has to send
keepalive packets. The keepalive & timeout mechanism can be
implemented in your application protocol (your server sets a timeout
before every read operation, and the client must explicitly send
keepalive data), or it can be implemented at the TCP level (your
server turns on the TCP keepalive option for each connection). See
https://www.google.com/#q=tcp+keepalive
On Thu, Feb 2, 2017 at 2:48 AM, Yuri Timenkov <***@timenkov.ru> wrote:
> Hi Robert,
>
>
>
> There are TCP keep-alive timeouts, but they are not usable in practice
> (could be between hours and days).
>
>
>
> What we did in this case is together with every read operation scheduled a
> deadline_timer which when fired calls shutdown() on the socket, so read
> operation will return with corresponding error code.
>
> Of course successful read operation should re-schedule timer.
>
>
>
> Note that timer callback is guaranteed to be executed only once, even when
> cancelled, so you need to handle error code properly.
>
>
>
> I would also recommend using strands to avoid concurrent access to shared
> resources (however calling shutdown() on socket invokes system call
> directly without changing C++ object state, compared to close(), so it’s
> relatively safe to call it in parallel with read).
>
>
>
> Hope this helps,
>
> Yuri
>
>
>
> From: Robert Bielik
> Sent: den 2 februari 2017 08:30
> To: asio-***@lists.sourceforge.net
> Subject: Re: [asio-users] No notification on terminated TCP connection?
>
>
>
> Thanks Ivan,
>
>
>
> As I might not be able to change anything on the client side, it needs to be
> able to detect severed connections. The client might crash f.i.
>
>
>
> As the server is “hanging” on the socket.async_read(…) call, shouldn’t be
> possible to detect a terminated connection ?
>
>
>
> I’m testing this right now on Windows (10) but the server will ultimately be
> deployed on Linux (Raspberry Pi). Are there platform differences with regard
> to this ?
>
>
>
> Regards
>
> /Robert
>
>
>
>
>
>
>
> Från: ivan kostov [mailto:***@gmail.com]
> Skickat: den 2 februari 2017 08:07
> Till: asio-***@lists.sourceforge.net
> Ämne: Re: [asio-users] No notification on terminated TCP connection?
>
>
>
> Hi Robert,
>
>
>
> I think in this case it is better to shutdown the connection (
> http://man7.org/linux/man-pages/man2/shutdown.2.html )instead of just
> closing it.
>
> Shutting down is like saying "Good bye" on the phone and only closing is
> like hanging up :)
>
>
>
> Sample code:
>
>
>
> boost::system::error_code ec;
>
> tcpSocket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
>
> tcpSocket.close();
>
>
>
> Best regards,
>
> Ivan
>
>
>
> 2017-02-02 7:39 GMT+01:00 Robert Bielik <***@dirac.com>:
>
> Hi all,
>
>
>
> I’m basing a TCP server on the async TCP echo server, i.e. I do async reads
> on the socket. But I expected to get a notification when a client drops its
> connection, i.e. async_read would call the supplied callback with an error,
> but it doesn’t. This is a problem since the server I make will only allow
> one active connection, so connection state detection needs to be rock solid.
>
>
>
> I’m using the Chrome Netcat app to connect. If I make an explicit
> disconnect, the session gets terminated properly. But not if I just close
> the Netcat app with an active connection.
>
>
>
> Regards
>
> /Robert
>
>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> asio-users mailing list
> asio-***@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/asio-users
> _______________________________________________
> Using Asio? List your project at
> http://think-async.com/Asio/WhoIsUsingAsio
>
>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> asio-users mailing list
> asio-***@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/asio-users
> _______________________________________________
> Using Asio? List your project at
> http://think-async.com/Asio/WhoIsUsingAsio
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-