Hi,
I am having issues with running out of memory or possibly running out of sockets. I have a HTTPListener running on a Fez Portal via wifi. If the network connection is unstable the connecting client will sometimes timeout. This causes some memory on the Fez to not be released. Eventually the HTTPListener throws an exception and it is not possible to connect anymore.
Does anyone of you clever forummembers have some ideas on what I can do to prevent this?
Server code (Thread.Sleep(500) causes the client to time out):
//Create a listener.
HttpListener listener = new HttpListener("http", 80);
listener.Start();
Debug.WriteLine("Listening...");
var clientRequestCount = 0;
while (true)
{
try
{
//Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
//Obtain a response object.
HttpListenerResponse response = context.Response;
Thread.Sleep(500);
//Construct a response.
System.GC.Collect();
var freeRam = GHIElectronics.TinyCLR.Native.Memory.ManagedMemory.FreeBytes;
var responseString = string.Format("<HTML><BODY> I am TinyCLR OS Server. " + "Client request count: {0}, FreeRAM: {1}</BODY></HTML>", ++clientRequestCount, freeRam);
Debug.WriteLine(responseString);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
//Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
var output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// When you close the response all the streams are closed and released.
response.Close();
}
catch (Exception)
{
}
}
listener.Stop();
Client code:
// Create the URL
Uri address = new Uri(“http://192.168.1.105”);
// Create request
HttpWebRequest request = HttpWebRequest.Create(address) as HttpWebRequest;
request.Method = "GET";
request.Timeout = 100;
request.KeepAlive = false;
// Get response
string responseString;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (var stream = new StreamReader(response.GetResponseStream()))
{
responseString = stream.ReadToEnd();
}
}
Debug output, the free RAM decreases until an exception occurs and all clients are refused:
I am TinyCLR OS Server. Client request count: 261, FreeRAM: 323792 I am TinyCLR OS Server. Client request count: 262, FreeRAM: 323008 I am TinyCLR OS Server. Client request count: 263, FreeRAM: 323008 I am TinyCLR OS Server. Client request count: 264, FreeRAM: 323008 I am TinyCLR OS Server. Client request count: 265, FreeRAM: 322480 I am TinyCLR OS Server. Client request count: 266, FreeRAM: 322480 I am TinyCLR OS Server. Client request count: 267, FreeRAM: 321952 I am TinyCLR OS Server. Client request count: 268, FreeRAM: 321952 I am TinyCLR OS Server. Client request count: 269, FreeRAM: 321424 I am TinyCLR OS Server. Client request count: 270, FreeRAM: 321424 I am TinyCLR OS Server. Client request count: 271, FreeRAM: 321424 I am TinyCLR OS Server. Client request count: 272, FreeRAM: 320896 I am TinyCLR OS Server. Client request count: 273, FreeRAM: 320896 I am TinyCLR OS Server. Client request count: 274, FreeRAM: 320368 I am TinyCLR OS Server. Client request count: 275, FreeRAM: 320368 I am TinyCLR OS Server. Client request count: 276, FreeRAM: 319840 I am TinyCLR OS Server. Client request count: 277, FreeRAM: 319840 I am TinyCLR OS Server. Client request count: 278, FreeRAM: 319840 I am TinyCLR OS Server. Client request count: 279, FreeRAM: 319312 I am TinyCLR OS Server. Client request count: 280, FreeRAM: 319312 I am TinyCLR OS Server. Client request count: 281, FreeRAM: 318784 I am TinyCLR OS Server. Client request count: 282, FreeRAM: 318784 I am TinyCLR OS Server. Client request count: 283, FreeRAM: 318784 I am TinyCLR OS Server. Client request count: 284, FreeRAM: 318256 #### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (5) #### #### Message: #### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::Accept [IP: 0000] #### #### System.Net.Sockets.Socket::Accept [IP: 0023] #### Exception thrown: 'System.InvalidOperationException' in GHIElectronics.TinyCLR.Devices.Network.dll The thread '' (0x5) has exited with code 0 (0x0).