Skip to content
Advertisement

boost asio notify server of disconnect

I was wondering if there is any way to notify a server if a client side application was closed. Normally, if I Ctrl+C my client side terminal an EOF-signal is sent to the server side. The server side async_read function has a handle which has boost::system::error_code ec argument fed into it. The handle is called when the server side receives EOF-signal which I can happily process and tell the server to start listening again.

However, if I try to cleanly close my client application using socket.shutdown() and socket.close() nothing happens and the server side socket remains open.

I was wondering, is there a way to somehow send an error signal to the server-side socket so I could then process it using the error code?

Advertisement

Answer

The approaches described in comments covers 99% of cases. It doesn’t work when client machine was (not gracefully) turned off, or network problems.

To get reliable notification of disconnected client you need to implement “ping” feature: to send ping packets regularly and to check that you received pong packets.

Advertisement