2

Since I use TCP for connection I wonder if I could use keep-alive signal as notification source for my app. Namely I would like to detect disconnection state (for given socket) at client side and server side.

Is it possible? If yes -- how to do it? So far I found options to set keep-alive intervals and Monitor method, but I don't see a way how to make a notification out of it.

On server side I would like to have info which client went dead of course.

astrowalker
  • 3,123
  • 3
  • 21
  • 40

1 Answers1

2

Is there a way to use TCP keepalive as an event?

No. TCP keepalives are not seen by the application.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    While it is true that the app does not see the keepalives themselves, it does see the after-effects. Once a keepalive times out, it invalidates the socket connection, and any subsequent read/write operations on the socket will fail with an error code that the app can see. – Remy Lebeau Aug 03 '17 at 18:46
  • @RemyLebeau Of course, otherwise they would be pointless, but you can't use that as an event either. – user207421 Aug 03 '17 at 21:36
  • 1
    @EJP: "*you can't use that as an event either*" - sure you can. When the keepalive elapses, it invalidates the socket connection. If the socket is asynchronous, that will notify the app with a close/error event. If the socket is non-blocking, that will signal `select()` with a read/error condition. If the socket is blocking, a blocked read/write will report an error. Those conditions can be handled by the app to signal whatever the `Monitor` is waiting on. – Remy Lebeau Aug 03 '17 at 21:44
  • This is incorrect. You can still use the KeepAlive as an event which will be signaled once all the probes have been sent out and not have been acknowledged by the remote peer. I have personally tested this on a real machine that i have a server running on and a client that connects from a virtual machine, what i did was paused the virtual machine to create a kind of "silent disconnection" later on after all the probes sent and unacknowledged, `GetQueuedCompletionStatus` returned with 0 and the last error was set to `ERROR_SEM_TIMEOUT`. – RCECoder May 04 '18 at 00:35