Getting SocketException when Wifi module network goes down then back up

Hi,

I am doing a little test with the Wifi module, as I am wanting it for a project that will be left in the wild for some time I want it to be able to try and re-connect if the network goes down.

Basically, it tries and finds the hard-coded network. If it does then it connects, at this point the timer is stopped.

If the NetworkDown event handler is hit then the timer is started again, which will scan for and join the network every 5 seconds.

If the network exists then this works great, it starts the timer, finds the network, joins it, hits the NetworkUp event handler, does a HttpRequest for google.co.uk and debug.prints the response.text.

But if I reset my router, the NetworkDown handler is hit (as expected), it starts searching, finds the network again after a while, joins it fine, hits the NetworkUp event handler but when it does the HttpRequest again on google.co.uk I get this:

#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (7) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::connect [IP: 0000] ####
    #### System.Net.Sockets.Socket::Connect [IP: 001d] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 015a] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a9] ####
    #### SocketException ErrorCode = 10038
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = 10038
    #### SocketException ErrorCode = 10038
    #### SocketException ErrorCode = 10038
    #### Exception System.Net.WebException - 0x00000000 (7) ####
    #### Message: connection failed
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 0168] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a9] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
    #### Exception System.Net.WebException - 0x00000000 (7) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
    #### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a9] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll

An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.

According to this blog post http://blogs.msdn.com/b/davidklinems/archive/2004/11/04/252689.aspx, error code 100038 means: SocketOperationOnNonSocket

So it seems that the socket hasn’t been created the second time, but Gadgeteer only finds out about this and throws the exception when a HttpRequest is attempted using WebClient.GetFromWeb().

Am I going about this wrong? Is it a problem with HttpRequest?

Thanks for your time,

Poncho

Hard to say. Show your code, can be a bug.

I did a quick search on the forum. We have two threads that has some related information and some suggestions. May be this can help:

http://www.tinyclr.com/forum/search/?q=10038