I’ve got a bit of a strange issue where I get the below exception being thrown after a few minutes of running. I can catch it fine, however when I do, the next time I call request.GetResponse() the method call never returns - when I pause in debugger, this is the last line avaialble in the call stack. Other threads are continuing to run ok, but this just seems to block indefinatlely.
The code is nothing special, it just calls to the same URL every minute within a while(true) loop, the response has .Close() explictly called on it each time, so it is being cleaned up, it is also wrapped in a using block, so dispose will be called on it also.
SocketException has a property ErrorCode. Catch that exception and check the ErrorCode - that will give you more information. Also please make sure that when you are done with the NetworkStream you close it properly (can’t see if you do it from your snippet).
Thanks. I’ve added the close to the stream as well, but still get the error occuring. The error code is 10035. I’ve found a few posts online with same error code, but no resolution for it. Any ideas??
I added KeepAlive = false to the web request, that seems to stop the thread from becoming blocked indefinatly. Exception still occurs, but can call the web request again ok now (well has been for the last couple of hours continuously).
I’ve put together this basic test. The only code omitted is that which sets up the wifi connection. The code just calls to google every second.
I am running this on Fez Cobra with WiFi module, using WiFi network.
...(Main)
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(RunBasicHttpTest);
....
static void RunBasicHttpTest(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable && WiFi.IsLinkConnected && IsStarted == false)
{
while (true)
{
try
{
using (HttpWebRequest request = HttpWebRequest.Create(@ "http://www.google.com") as HttpWebRequest)
{
request.Method = "GET";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (Stream receiveStream = response.GetResponseStream())
{
// simulate reading the stream.
using (StreamReader readStream = new StreamReader(receiveStream))
{
string s = readStream.ReadToEnd();
readStream.Close();
receiveStream.Close();
response.Close();
}
}
}
}
Debug.Print(DateTime.UtcNow.ToString());
}
catch (WebException webEx)
{
Debug.Print("Web Exception Occured");
if (webEx.InnerException is SocketException)
{
SocketException sock = webEx.InnerException as SocketException;
Debug.Print("Socket error code: " + sock.ErrorCode.ToString());
}
}
catch (Exception ex)
{
Debug.Print("Exception of type: " + ex.GetType().FullName);
}
finally
{
Thread.Sleep(1000);
}
}
}
}
This ran ok for 25 mintutes, the last parts of the output are below, after this the thread that was calling the URL is blocked on the line below (Paused debugger and this is where the call stack ends)
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
Architect, normally the URL is my own without dos / flooding protections, but either way I wouldn’t expect the tcp stack to have issues with that (and cause clr to lock up), worst case a timeout exception or http error code.
Any chance someone could run above example on a fez with wifi overnight and let me know the result? I can’t see what errors I could be making from a code pov to cause this.