Note that there are some explanatory texts on larger screens.

plurals
  1. POBad performance when offering files to download with HttpListener
    primarykey
    data
    text
    <p>I'm trying to create a simple web server using <code>HttpListener</code> in <code>C#</code> and offer files to download. I'm seeing really bad transfer rates especially compared to copying that same file from a share. Is this known to <code>HttpListener</code> and what can be done to improve it?</p> <p>Here's some additional info about research I have done about the problem. Download rates improve a lot when connecting locally but copying the file is done almost instantly in this case, so it's hard to measure a difference ratio. When connecting remotely (<code>LAN</code> environment, machines right next to each other) however, the transfer time is roughly 25x the time of a simple file copy from a share. The available network bandwidth doesn't seem to be used to speed this up.</p> <p>I've found some other questions and discussions about <code>HttpListener</code> that seem to indicate similar issues, see here:</p> <p><a href="https://stackoverflow.com/questions/4455887/httplistener-vs-native-http-performance">HttpListener vs native performance</a></p> <p><a href="https://stackoverflow.com/questions/6907841/httplistener-performance-optimization">HttpListener Performance Optimization</a> (this is not regarding downloads however)</p> <p><a href="http://msdn.microsoft.com/de-de/library/ms229710.aspx" rel="nofollow noreferrer">MSDN docs</a> also state that <code>HttpListener</code> is based on <code>http.sys</code> which allows for bandwidth throttling. Could it be that some unwanted bandwidth throttling is going on here or is there something wrong with my code? On the machines I've tested with (Windows 7 and Windows 2008 R2), no IIS was present.</p> <p>In my sample, I'm starting an <code>HttpListener</code> like so:</p> <pre><code> HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://*:80/"); listener.Start(); </code></pre> <p>Here's the code for my simple file download:</p> <pre><code> HttpListenerResponse response = null; try { HttpListenerContext context = listener.GetContext(); response = context.Response; using( FileStream fs = File.OpenRead( @"c:\downloadsample\testfile.pdf" ) ) { byte[] buffer = new byte[ 32768 ]; int read; while( ( read = fs.Read( buffer, 0, buffer.Length ) ) &gt; 0 ) { response.OutputStream.Write( buffer, 0, read ); } } } finally { if( response != null ) response.Close(); } </code></pre> <p>(edit: fixed some links...)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload