"Specified network name is no longer available" in Httplistener
Asked Answered
M

5

15

I have built a simple web service that simply uses HttpListener to receive and send requests. Occasionally, the service fails with "Specified network name is no longer available". It appears to be thrown when I write to the output buffer of the HttpListenerResponse.

Here is the error:

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

and here is the guilty portion of the code. responseString is the data being sent back to the client:

buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

response.ContentLength64 = buffer.Length;
output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);

It doesn't seem to always be a huge buffer, two examples are 3,816 bytes and, 142,619 bytes, these errors were thrown about 30 seconds apart. I would not think that my single client application would be overloading HTTPlistener; the client does occasionally sent/receive data in bursts, with several exchanges happening one after another.

Mostly Google searches shows that this is a common IT problem where, when there are network problems, this error is shown -- most of the help is directed toward sysadmins diagnosing a problem with an app moreso than developers tracking down a bug. My app has been tested on different machines, networks, etc. and I don't think it's simply a network configuration problem.

What may be the cause of this problem?

Moisture answered 9/1, 2009 at 19:48 Comment(3)
Do you have any code you can post? Something stripped down showing the error would be very helpful.Joellejoellen
added some more details, austin.Moisture
No, no resolution yet, @CraftyFella.Moisture
O
7

I'm getting this too, when a ContentLength64 is specified and KeepAlive is false. It seems as though the client is inspecting the Content-Length header (which, by all possible accounts, is set correctly, since I get an exception with any other value) and then saying "Whelp I'm done KTHXBYE" and closing the connection a little bit before the underlying HttpListenerResponse stream was expecting it to. For now, I'm just catching the exception and moving on.

Overflight answered 4/9, 2009 at 17:11 Comment(3)
New thoughts about that after ten years? Do you know what is the inner reason of System.Net.HttpListenerException: Specified network name is no longer available?Meteorology
@VitalyZdanevich I was never able to attribute it to a problem a user actually experienced. The application that was swallowing it as a Windows service running HttpListener swallowed it for 7 years without incident before I rehosted it in a traditional ASP.NET web app so I could move it to AzureOverflight
I'm getting this too. seems to only happen when returning http 500Selfassertion
B
1

The problem occurs when you're trying to respond to an invalid request. Take a look at this. I found out that the only way to solve this problem is:

listener = new HttpListener();
listener.IgnoreWriteExceptions = true;

Just set IgnoreWriteExceptions to true after instantiating your listener and the errors are gone.


Update:
For a deeper explanation, Http protocol is based on TCP protocol which works with streams to which each peer writes data. TCP protocol is peer to peer and each peer can close the connection. When the client sends a request to your HttpListener there will be a TCP handshake, then the server will process the data and responds back to the client by writing into the connection's stream. If you try to write into a stream which is already closed by the remote peer the Exception with "Specified network name is no longer available" will occur.

Bureaucracy answered 30/11, 2020 at 13:58 Comment(0)
Y
0

I've only gotten this particular exception once so far when using HttpListener. It occurred when I resumed execution after my application had been standing on a breakpoint for a while.

Perhaps there is some sort of internal timeout involved? Your application sends data in bursts, which means it's probably completely inactive a lot of the time. Did the exception occur immediately after a period of inactivity?

Your answered 5/6, 2009 at 7:36 Comment(0)
M
0

Same problem here, but other threads suggest ignoring the Exception.

C# problem with HttpListener

May be that's not the right thing to do.

Mungovan answered 18/11, 2011 at 7:4 Comment(0)
N
0

For me I find that whenever the client close the webpage before it load fully it gives me that exception. What I do is just add a try catch block and print something when the exception happen. In another word I just ignore the exception.

Nondescript answered 27/7, 2020 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.