user704565
user704565

Reputation:

ASIO - Detect dead connection

I am trying to create an asynchronous server in C++ using Boost ASIO library, it goes by standard model provided at their webpage, ioservice and worker (session) I am using something like this:

asio::async_read(socket,buffer(data,datalen),[=]( error , len ){
     if(error || len==0) disconnect(); else processRequest(data,len);
})

Firstly, it works just fine, when user opens and closes connection - OS does the job.

But problem comes when user disconnects from internet by force, socket remains in "ESTABLISHED" state, though user is already gone, yeah, how would OS handle sending TCP close packet, when internet is not available anymore?

Also problem comes with telephony internet networks, user travels, switches zone from HSDPA to no internet and finally GPRS, old HSDPA socket remains "ESTABLISHED", though user already doesn't use it.

How to detect dead connections like this?

Upvotes: 0

Views: 1827

Answers (1)

stefan
stefan

Reputation: 3759

  • When the client application closes the socket your read handler will be called immediately
  • When the client application crashes but the client OS is still online your read handler will be called after the client OS timeout (one minute or so)
  • When the client computers cable is unplugged there is no notification at all. You can implement a keep alive mechanism at application layer or use the TCP built-in keep-alive. I'd recommend to do your own as you have more control (e.g. reconnect a session). In principal you do an async_write which would result in your write handler being called with an error.

Upvotes: 2

Related Questions