views:

108

answers:

1

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.

A: 

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