HttpListener simultaneous connections give HttpListenerException

Hope someone can shed some light on this.

I have seen lots of similar posts, but they tend to point to a client side restriction of 2 simultaneous connections, but I need to know what I can change on the server side.

I have a C# webserver which uses HttpListener

Note the Thread.Sleep(100) line which is to simulate getting real data to return.

The code snippet is

HttpListener listener = new HttpListener();

while (true)
{
try
{
HttpListenerContext context = listener.GetContext();
byte[] buffer = Encoding.ASCII.GetBytes("hi");
Thread.Sleep(100);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
catch (Exception e)
{
log(e.ToString());
}
}

From IE this works fine, and shows "hi" in the browser, but hold down F5 to force a continuous stream of connections, and a HttpListenerException is caught, which I then log as follows

System.Net.HttpListenerException: An operation was attempted on a nonexistent network connection
at System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at form.webserver() in C:\source\prog.cs:line 62

Now... remove the Thread.Sleep(100) and it works fine.

And, if I replace the Thread.Sleep(100) with the real code which gets the real HTML to return, is also fails.

Single calls are OK, but multiple sequential calls with F5 fail every time.

Any ideas much appreciated.

Rid



Answer this question

HttpListener simultaneous connections give HttpListenerException

  • pardgr8

    On second thoughts, not really the best way to leave it.

    The browser is just asking for more stuff, and somewhere along the line a connection is being dropped, but it isn't really doing anything out of the ordinary.

    I can't find anything useful on google for

    System.Net.HttpListenerException: The specified network name is no longer available
    at System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)

    Pity the error isn't a bit more descriptive


  • GlennB

    >I'll just leave it there.

    Yup, that's what I did as well.


  • Kate Wells

    Shiv

    Thanks again for your reply.

    With the second scenario, the error is sometimes

    System.Net.HttpListenerException: An operation was attempted on a nonexistent network connection
    at System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
    at form.webserver() in C:\source\prog.cs:line 62

    and sometimes

    System.Net.HttpListenerException: The specified network name is no longer available
    at System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
    at form.webserver() in C:\source\prog.cs:line 62

    more likely to be the second one.

    I understand your explanation of how the browser issues the HTTP requests, but surely this is a different situation to before where the user refreshes the page I can expect that it makes sense that they no longer want the first connection, so it is closed, but if the browser is issuing multiple requests, then why would it close any until it gets what it wants

    I'm not using asynch, just what I posted oringinally. This code came from Microsoft, so i would expect it to do the job.

    Perhaps there is a paremeter on the server side I need to use.

    Cheers

    Rid


  • Lorren

    Rid,

    This is expected since, when you hold down the F5 key in I.E. the "previous" connections are closed (client side). So when the server attempts to respond to that "previous" connection, there isn't any.

    Indirectly, your "test" is not a valid test. Indeed, in the real world you will have to trap this particular expection and move on since a user could (and will) very well do the same thing..

    In order to test this, you could build a multi-threaded http client app, spawn multiple threads and have each one make continuous requests to your server.


  • Radoslav Č&#225&#59;p

    >but surely this is a different situation to before

    Yes, it is.

    I've not seen that second exception you've listed.

    Is the browser not able to get the resources (the collateral files) it's looking for What I mean is, if you swallow the exception (which you're technically doing from the code you posted) does the browser not render the page (or render it correctly).

    I don't remember any server side parameter that needs to be set.

    Frankly, I had similar issues like these but had to move away from the HttpListener anyway since I needed to support platforms other than XP SP2 and W2K3. So I ended up making my own web server using sockets that supports the notion of Handlers, Request, Response etc.

    I do rmember reading an MDSN magazine article that used the HttpListener that hosted the ASP.NET pipeline. You might want to take a look at that.

    Shiv.


  • José Valim

    Rid,

    >so several buttons, several calls

    That's right..

    Let me explain. The way the http protocol works is that in the first request the browser will get the "html" part. While the browser is parsing this html in order to render the page, it will go out to get all the other collateral (images, css files, javascript files etc.).

    It will make one request for every such "file" it needs to get. If you're using relative urls for these "files" then your server needs to be able to respond with these files as well.

    So effectively, there are multiple requests being made, virtually at the same time.

    Now I haven't looked at your code closely. Are you using the asynch methods If not, you should try with those methods. The original behaviour (closed connections) is to be expected mind you, but the server will be in a better postion to handle multiple simultaneous requests and so you may not see the issue to do with collaterals.

    By the way, is the exception the same


  • Avner Kashtan

    Hi,

    I am facing the same two exceptions you mentioned. Did you find any solution to it ...I am gonna mad by these exceptions...my code is almost similar to your's except that i am doing it in .NET.. and i am making a proxy server.

    Regards,

    Jan


  • Timon Christl

    Shiv

    Thanks for your help.

    The page does render correctly, but it worries me that ANY exception is being raised.

    I'll just leave it there.

    Cheers

    Rid


  • Torns

    Shiv

    Thanks for your reply.

    That makes sense, but when I remove the Thread.Sleep(100) and replace

    byte[] buffer = Encoding.ASCII.GetBytes("hi");

    with a call to a routine that send back the real HTML, it fails even with a single call from IE6.

    I have narrowed it down to a single line of the HTML

    .button {filter:alpha(opacity=75); position: relative; top: 0; left: 0; background-image:url(images/button.jpg);background-repeat:no-repeat; WIDTH:300; HEIGHT:48px; FONT-WEIGHT: bold; FONT-SIZE: 18pt; font-family: arial; color: #f2f2f2; padding-left: 10; padding-top: 4}\n

    which as you can see is actually some CSS

    I think that as soon as the HTML hits the browser, it interprets the CSS, and any object within the HTML which has a class=button, immediately makes another call to fetch the background-image, so several buttons, several calls, and perhaps these are cancelling each other out as "previous connections"

    Does this makes sense

    Cheers

    Rid


  • HttpListener simultaneous connections give HttpListenerException