Detect client disconnect with HttpListener

后端 未结 3 1594
独厮守ぢ
独厮守ぢ 2021-02-14 06:31

I have an application that uses HttpListener, I need to know when the client disconnected, right now I have all my code inside a try/catch block which is pretty ugly and not a g

3条回答
  •  野的像风
    2021-02-14 06:51

    Short answer: you can't. If a client stops talking, the underlying socket may stay open and won't ever close; it'll just timeout. The way to detect this is to attempt to perform an action on that connection and if the connection is no longer valid, it'll throw some sort of exception depending on what happened. If you use HttpListener asynchronously, it may clean up your code a bit in terms of a try/catch but unfortunately that's what you're stuck with. There is no event that will fire if the client disconnects.

提交回复
热议问题