2

I've implemented a task using the async Sockets pattern in Silverlight 3. I started with Michael Schwarz's implementation and built on top of that. So basically, my Silverlight app establishes a persistent socket connection to a device and then data flows both ways as necessary between the device and the Silverlight app.

One thing I am struggling with is how to detect disconnection. I could think of 2 approaches:

  1. Keep-Alive. I know this can be done at the Sockets level, but I am not sure how to do this in an async model. How would the Socket class let me know there has been a disconnection.

  2. Manual keep alive. Basically, I am having the Silverlight app send a dummy packet every 20 seconds or so. If it fails, I'd assume disconnection. However, incredibly, SocketAsyncEventArgs.SocketError always reports success, even if I simply unplug the device that the Silverlight app is connected to. I am not sure whether this is a bug or what or perhaps I need to upgrade to SL4.

Any ideas, direction or implementation would be appreciated.

AngryHacker
  • 54,471
  • 90
  • 289
  • 523
  • Can you poll your socket? http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket – sbenderli Jul 02 '10 at 13:56
  • @sbenderli Method is not available in Silverlight – AngryHacker Jul 02 '10 at 20:45
  • how about trying to send an empty packet and catching the socket error every once in a while? sort of like a manual polling? – sbenderli Jul 03 '10 at 16:28
  • @sbenderli Believe it or not that does not actually work. I send out a packet and the result comes in as success for at least a minute after I disconnected the target device. See my #2. – AngryHacker Jul 04 '10 at 04:39
  • Of course it doesn't work. @sbenderli, you can't send an empty packet over TCP. You can issue a zero-length write, but that's the end of it, nothing is sent over the network, so there is nothing to acknowledge, so there is no ACK, or lack of it. So it doesn't work. – user207421 Jul 04 '10 at 05:10

1 Answers1

0

I ended up going with a heartbeat packet that requires an answer from the target. Basically, I send a predefined dummy packet and the target device must respond with the same, otherwise the connection is considered invalid.

AngryHacker
  • 54,471
  • 90
  • 289
  • 523