0

Lets say that I have a ASP.NET application and I hold a connection for 10 seconds. In that time the client lost network access.

Can I detect that before returning the response?

Hristo Kolev
  • 1,236
  • 1
  • 14
  • 28
  • Maybe. Maybe not. It's actually kinda complicated. Presumably you're trying to avoid doing some work if the client's no longer there to receive it? – Roger Lipscombe Feb 13 '17 at 18:42
  • Actually, no. I'm doing message transmission and want to know that the client received the message. I have an implementation of long polling. Currently I'm confirming that the message was received by sending another request, but that's not very performance friendly. And doesn't help me detect dead clients. – Hristo Kolev Feb 13 '17 at 19:09
  • If you want reliable messaging, you're going to need application-level ACKs. You can't reliably detect that a TCP connection has gone away. – Roger Lipscombe Feb 13 '17 at 22:41

2 Answers2

1

You can't detect lost connection "in HTTP", because it is an application layer protocol and too abstract for that.

But you could detect that your client has closed the connection on a network level. I'm not familiar with ASP.net, but you could start from here: Instantly detect client disconnection from server socket.

Community
  • 1
  • 1
Alexander Svetkin
  • 1,119
  • 12
  • 15
0

You can check the IsClientConnectedProperty. For example

void HeavyProcessing()
{
    while (longLoop)
    {
        if (!HttpContext.Current.Response.IsClientConnected) Response.End();
        //Do heavy processing
    }
}
John Wu
  • 44,075
  • 6
  • 37
  • 69