"Remote host closed the connection" in Response.OutputStream.Write

This code transfers large files to our users:

// Open the file. iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); // Total bytes to read: dataToRead = iStream.Length; // Read the bytes. while (dataToRead > 0) { // Verify that the client is connected. if (Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer, 0, 10000); // Write the data to the current output stream. Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output. Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { //prevent infinite loop if user disconnects dataToRead = -1; } } 

From time to time we get this exception:

 The remote host closed the connection. The error code is 0x80072746 

Here is the full stack trace:

 Stack Trace: at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async) at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal) at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush) at System.Web.HttpResponse.Flush(Boolean finalFlush) at System.Web.HttpResponse.Flush() at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size) at System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count) at BIS.DocumentBus.Controls.DocumentViewer.StreamFile(String filepath) 

We have never had evidence that users are having trouble downloading our files and plan to simply ignore this exception.

Any idea what is the source of this problem? Is it safe to ignore?

+21
c # streaming
May 6 '09 at 19:57
source share
4 answers

This exception means that the client downloading the file broke the connection before the file completed the download. those. the client went to another page or just closed the browser.

I will try to move the if (Response.IsClientConnected) after iStream.Read . Even if you do, I think there might still be a chance to get this error if the connection is broken while the OutputStream.Write method is still working.

+17
May 6 '09 at 20:06
source share

There are several different reasons for this. I can think of three:

One fills the buffer with a volume of about 2 GB, but it should not be here, since you are regularly cleaned.

Another really is what is described in the answer you previously accepted. This is very difficult to reproduce, so I did not assume that it was necessarily wrong.

Another possible case, and the one on which I would bet, is that the executeTimeout value is exceeded, which will initially throw a ThreadAbortException, but this, in turn, can cause Flush () to fail, which will turn into an exception, indicated

+12
Sep 16 '10 at
source share

Increase the timeout execution in the httpRuntime web.config element.

If the user downloads a large file during a slow connection, the request will eventually expire.

+4
Sep 20 '10 at 21:30
source share

I am posting this answer because it can help others and save some important time.

In my case, Response.Buffer = true in the boot method (in the very first statement), the problem was solved.

thank

+2
Nov 03 '13 at 15:04
source share



All Articles