I’m getting a System.Net.Sockets.SocketException whenever I call WebServer.StartLocalServer. However, if I set a breakpoint on the StartLocalServer call and continue when it hits the breakpoint, or if I insert a sleep just before the StartLocalSever call (2270ms or more), the exception does not occur.
Here is the ProgramStarted code:
void ProgramStarted()
{
Debug.Print("Program Started");
ethernetENC28.UseThisNetworkInterface();
ethernetENC28.UseStaticIP("192.168.0.2", "255.255.255.0", "192.168.0.1", new string[] { "8.8.8.8" });
ethernetENC28.NetworkUp += ethernetENC28_NetworkUp;
ethernetENC28.NetworkDown += ethernetENC28_NetworkDown;
}
Here is the NetworkUp event handler code:
void ethernetENC28_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
{
Debug.Print("Network is UP.");
Debug.Print(ethernetENC28.NetworkSettings.IPAddress);
Debug.Print("SetUpWebEvent and assign handler");
WebServer.SetupWebEvent("c").WebEventReceived += webEvent_WebEventReceived;
WebServer.DefaultEvent.WebEventReceived += DefaultEvent_WebEventReceived;
//Debug.Print("Sleeping...");
//Thread.Sleep(2270); // this delay (or higher) will prevent the exception.
Debug.Print("Start web server");
WebServer.StartLocalServer(ethernetENC28.NetworkSettings.IPAddress, 80);
Debug.Print("Leaving NetworkUp handler.");
}
Here is the output if I do NOT set a breakpoint or introduce a delay before the StartLocalServer call:
Program Started
Network is UP.
192.168.0.2
SetUpWebEvent and assign handler
Start web server
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::bind [IP: 0000] ####
#### System.Net.Sockets.Socket::Bind [IP: 0016] ####
#### Gadgeteer.Networking.WebServerManager+Server::StartLocal [IP: 002d] ####
#### Gadgeteer.Networking.WebServerManager::StartServer [IP: 0012] ####
#### Gadgeteer.Networking.WebServer::StartLocalServer [IP: 0014] ####
#### HydraTest002.Program::ethernetENC28_NetworkUp [IP: 0062] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 001d] ####
#### SocketException ErrorCode = 10022
#### SocketException ErrorCode = 10022
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10022
#### SocketException ErrorCode = 10022
Here is the output if I DO set/continue a breakpoint or introduce a delay of 2270ms or more:
Program Started
Network is UP.
192.168.0.2
SetUpWebEvent and assign handler
Sleeping…
Start web server
Web server started at http://192.168.0.2:80/
Leaving NetworkUp handler.
Running 4.3.6.0. The ENC28 is on socket 3 of the Hydra. Note that I can successfully ping 192.168.0.2 (the address of the Gadgeteer device) from another computer on the network.
ADDED NOTE: I just tried the same thing on a Spider with a J11D Ethernet module, and am getting the exact same results. Same exception when there’s no breakpoint or delay, but the exception disappears when I break/continue at the StartLocalServer call or introduce enough of a delay before the call.
Any thoughts on what’s happening here? I’m sure I must be missing something simple.
Thanks.