How to detect a Socket disconnection?

Posted by AngryHacker on Stack Overflow See other posts from Stack Overflow or by AngryHacker
Published on 2010-05-19T17:04:18Z Indexed on 2010/05/19 22:30 UTC
Read the original article Hit count: 166

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.

© Stack Overflow or respective owner

Related posts about silverlight-3.0

Related posts about sockets