How to tell when a Socket has been disconnected

后端 未结 5 1310
滥情空心
滥情空心 2021-02-05 11:36

On the client side I need to know when/if my socket connection has been broken. However the Socket.Connected property always returns true, even after the server side has been d

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 11:58

    Just write to your socket as normal. You'll know when it's disconnected by the Exception that says your data couldn't be delivered.

    If you don't have anything to write...then who cares if it's disconnected? It may be disconnected now, but come back before you need it - why bother tearing it down, and then looping a reconnect until the link is repaired...especially when you didn't have anything to say anyway?

    If it bothers you, implement a keep alive in your protocol. Then you'll have something to say every 30 seconds or so.

提交回复
热议问题