我知道 Response.IsClientConnected
但在我的场景中它有很大的滞后。代码:
// 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;
}
}
问题是客户端实际接收到的字节数比我的
bytesSent
小得多。似乎当客户端断开连接时,我的程序发现情况有很大的滞后(并继续增加 bytesSent
),这是因为 ASP.NET 告诉我这种情况(客户端断开连接)很晚。是否有任何可靠的方法可以找出客户端何时断开连接(实时)?
最佳答案
您正在通过 HTTP 传输,不是吗?如果是,则由于 HTTP 协议(protocol)的无状态性,没有办法。您唯一需要帮助的是超时,您已经在使用它。
关于c# - 如何找到一个asp.net客户端立即断开连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5056988/