I'm aware of Response.IsClientConnected
but in my scenario it has a great lag. Code:
// sample code for sending a dynamic file in chuncks
long bytesSent = 0;
while (continueSending)
{
if (!AskReceiverToContinue())
break;
if (!Response.IsClientConnected)
break;
// Your suggestion goes here... :)
try
{
Response.OutputStream.Write(buffer, 0, buffer.Length);
bytesSent += buffer.Length;
}
Catch
{ // To my experience, this is more reliable than Response.IsClientConnected
continueSending = false;
}
}
The problem is the actual received bytes by client is very smaller in amount than my bytesSent
. It seems when a client gets disconnected my program finds out the situation with a great lag (and continue increasing bytesSent
) and this is because ASP.NET tells me the situation (client is disconnected) late.
Is there any reliable method for finding out when a client has been disconnected (real-time) ?
You are transfering over HTTP, aren't you? If yes, there is no way due to the statelessness of the HTTP protocol. Only thing you have to help you is the timeout, which you are already using.
来源:https://stackoverflow.com/questions/5056988/how-to-find-an-asp-net-client-is-disconnected-immidiately