如何检测Socket断开连接?
我已经使用 Silverlight 3 中的异步套接字模式实现了一项任务。我从 Michael Schwarz 的实现 并在此基础上构建。基本上,我的 Silverlight 应用程序会建立与设备的持久套接字连接,然后数据根据需要在设备和 Silverlight 应用程序之间双向流动。
我正在努力解决的一件事是如何检测断开连接。我可以想到两种方法:
保持活动。我知道这可以在套接字级别完成,但我不确定如何在异步模型中执行此操作。 Socket 类如何让我知道已断开连接。
手动保持活动状态。基本上,我让 Silverlight 应用程序每 20 秒左右发送一个虚拟数据包。如果失败,我会假设断开连接。然而,令人难以置信的是,SocketAsyncEventArgs.SocketError 总是报告成功,即使我只是拔掉 Silverlight 应用程序连接到的设备也是如此。我不确定这是否是一个错误或什么,或者我可能需要升级到 SL4。
任何想法、方向或实施将不胜感激。
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:
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终得到了一个需要目标回答的心跳包。基本上,我发送一个预定义的虚拟数据包,目标设备必须做出相同的响应,否则连接被视为无效。
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.