How to find an asp.net client is disconnected immidiately

家住魔仙堡 提交于 2019-12-21 05:29:12

问题


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) ?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!