Hi guys,
Thanks for the advice.
After splitting into various threads (like one for connecting to the wifi and another for handling the data send), as well as now using the Toolbox.HTTP_Client reference, I seem to have resolved a timeout causing the Netduino/wifly module to hang
I have been able to run a test with over 700 HTTP Get requests and although there were a number of timeouts none of them caused the Netduino to hang/crash!!
Only problem I have now though is that I run out of memory... and I can't understand why, but I think that's for another thread!
Please find my code below (please ignore all the "Debug.GC(true);" lines as it's part of my efforts to figure out the memory issue) , I hope it helps someone else. Thanks to everyone for their input!!
using System;using System.Collections;using System.IO;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using Toolbox.NETMF.Hardware;using Toolbox.NETMF.NET;namespace testToolbox21240{ public class Program { static uint intCounter = 0; static uint intTimeoutCounter = 0; static WiFlyGSX WifiModule; static string strWriteFile = "dataone"; static string strReadFile = "datatwo"; static ArrayList counterDataArray = new ArrayList(); public static void Main() { connectToWifi(); populateArray(); //httpSendData(); Thread.Sleep(Timeout.Infinite); } public static void populateArray() { Debug.Print("Populate array from: " + strReadFile); using (var filestream = new FileStream(@"SD" + strReadFile + ".txt", FileMode.Open)) { string strSendLine; StreamReader sr = new StreamReader(filestream); while ((strSendLine = sr.ReadLine()) != null) { counterDataArray.Add(strSendLine); } sr.Close(); sr.Dispose(); Debug.GC(true); } Debug.Print("send data from array"); foreach (string value in counterDataArray) { httpSendData(value); Debug.EnableGCMessages(true); } counterDataArray.Clear(); Debug.GC(true); cleanUp(); } public static void cleanUp() { if (strWriteFile == "dataone") { strWriteFile = "datatwo"; strReadFile = "dataone"; } else { strWriteFile = "dataone"; strReadFile = "datatwo"; } Debug.GC(true); Debug.Print("Free Memory: " + Debug.GC(true).ToString()); Debug.Print("Record count: " + intCounter); Debug.GC(true); populateArray(); } public static void connectToWifi() { //SimpleSocket Socket = new WiFlySocket("myURL.com", 80, WifiModule); //httpSendData(Socket); WifiModule = new WiFlyGSX(); WifiModule.EnableDHCP(); WifiModule.JoinNetwork("mySSID", 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, "myPassword"); Thread.Sleep(3500); // Showing some interesting output Debug.Print("Local IP: " + WifiModule.LocalIP); Debug.Print("MAC address: " + WifiModule.MacAddress); SNTP_Client TimeClient = new SNTP_Client(new WiFlySocket("ntp2.is.co.za", 123, WifiModule)); Thread.Sleep(500); TimeClient.Synchronize(); Thread.Sleep(500); Debug.Print("DateTime.Now: " + DateTime.Now.AddHours(2)); Thread.Sleep(3500); Debug.GC(true); } public static void httpSendData(string queryStringData) { SimpleSocket Socket = new WiFlySocket("myURL.com", 80, WifiModule); HTTP_Client WebSession = new HTTP_Client(Socket); try { HTTP_Client.HTTP_Response Response = WebSession.Get("/default.aspx?" + queryStringData); // Fetches a response header Debug.Print("value: " + queryStringData); Debug.GC(true); } catch (System.ApplicationException e) { Debug.Print("Error Code: " + e); intTimeoutCounter++; Debug.Print("intTimeoutCounter: " + intTimeoutCounter); using (var filestream = new FileStream(@"SDlog.txt", FileMode.Append)) { StreamWriter sw = new StreamWriter(filestream); sw.WriteLine("DateTime: " + DateTime.Now.AddHours(2) + "rnError Code: " + e + "rnintTimeoutCounter: " + intTimeoutCounter+ "rn"); sw.Flush(); sw.Close(); Debug.GC(true); } Thread.Sleep(1500); Debug.GC(true); httpSendData(queryStringData); } finally { Socket.Close(); //intCounter++; Debug.GC(true); } } }}