Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Static IP address changes and HTTP listeners


  • Please log in to reply
No replies to this topic

#1 Pyt

Pyt

    Member

  • Members
  • PipPip
  • 18 posts

Posted 01 January 2014 - 04:25 PM

In reference to my previous post (http://forums.netdui...rnet-circuitry/), where I've failed miserably to change the IP configuration across a device reboot without unplugging said device, I came across another problem, which may be related.

Since I couldn't get it to work with a simple reboot, I tried dynamically changing the static IP address. Here's the extent of the code:

public static void Main() {    NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP("10.0.0.10", "255.255.255.0", "10.0.0.1");    Debug.Print("IP = " + IPAddress.GetDefaultLocalAddress());    HttpListener listener = new HttpListener("http");    listener.Start();    HttpListenerContext context = listener.GetContext(); // blocks until we get a request    Debug.Print(context.Request.Headers.ToString());    context.Close();    listener.Close();    Thread.Sleep(1000);    NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP("10.0.0.11", "255.255.255.0", "10.0.0.1");    listener = new HttpListener("http");    bool nok = true;    while (nok) {        try {            listener.Start();            Debug.Print("Success");            Debug.Print("IP = " + IPAddress.GetDefaultLocalAddress());            nok = false;        } catch (Exception ex) {            Debug.Print("Failed with " + ex.ToString());            Thread.Sleep(100);        }    }    context = listener.GetContext();    Debug.Print(context.Request.Headers.ToString());    context.Close();}

In essence, I'm programming the network interface with a static IP address (10.0.0.10), starting an HTTP listener, sending a request to http://10.0.0.10 through my browser, printing the headers, then closing the context and the listener. So far, so good.

Then I change the IP address and try again, to no avail: the new HTTP listener will never start, throwing a SocketException everytime.

Now if I comment out the second EnableStaticIP (line 11), the new listener will fail to start exactly 4 times in a row, throwing a SocketException again, and succeed the 5th time ! By the way, if I reuse the same listener as the first time, not creating a new one, I get the exact same behavior (in other words. listener.Stop() followed by listerner.Start() fails).

 

Here's a transcript of the debug window for this last case:

IP = 10.0.0.10Accept: text/html, application/xhtml+xml, */*Accept-Language: en-US,en;q=0.8,fr-FR;q=0.5,fr;q=0.3User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; MAARJS; rv:11.0) like GeckoAccept-Encoding: gzip, deflateHost: 10.0.0.10DNT: 1Connection: Keep-AliveAuthorization: Basic YWRtaW46cGFzc3dvcmQ=A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dllThe thread '<No Name>' (0x5) has exited with code 0 (0x0).A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dllFailed with System.Net.Sockets.SocketExceptionA first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dllFailed with System.Net.Sockets.SocketExceptionA first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dllFailed with System.Net.Sockets.SocketExceptionA first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dllFailed with System.Net.Sockets.SocketExceptionSuccessIP = 10.0.0.10

In the first case where I change the IP address, I get the same transcript, except that the "Success" message never comes and it keeps failing over and over.

 

So bottom line:

  • Why doesn't [font="'courier new', courier, monospace;"]{ listener.Stop(); listener.Start(); }[/font] work right away ?
  • What does it take to change the IP address of the network interface and still be able to get an HttpListener that will start properly without having to power cycle the device?

Thanks for your help.

Pyt.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.