N+2 Hangs on HttpWebRequest.GetRequestStream() - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
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

N+2 Hangs on HttpWebRequest.GetRequestStream()

COSM HttpWebRequest

Best Answer Giuliano, 27 February 2013 - 05:34 AM

Hi Chris,

 

Thank you very much for you reply. You nailed it, it was the MAC Address being reserved in the router (see snapshot below), even thoght I was changing the Static IP address on the N+2 using MFDeploy Network Configuration to something else than 3.2.72.20, seems like the router was blocking the traffic the second time around coming from the same MAC address withing a different IP address.

 

Posted Image

 

Man, I spent so much time on this issue, what a relief!!!

 

dbradshaw, maybe you have the same issue I had.

 

Cheers!

Go to the full post


  • Please log in to reply
16 replies to this topic

#1 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 25 February 2013 - 06:16 AM

I have a program that runs on my N+2 and captures temperature and humidity values, then it pushes those values to COSM (formaly known as Pachube). Most of the time a experiment an issue where the N+2 freezes on the HttpWebRequest.GetRequestStream() and just sits there forever on that line of code. Then even if I reset the N+2 and re-run the program again freezes on the same line of code. So, what I have to do is open MFDeploy -> Network Configuration and change its Static IP addres to something that is available in my network, so no other devices in my network are using that IP address. Then the N+2 starts pushing data to COSM once again and no problems. Then if for some reason I stop the program and re-run it, the N+2 hangs again on the same line of code. It seems like the IP gets blocked or something and cannot be used anymore. Then I go the the N+2 static IP address to other available IP addres, different than the 2 previous used IPs and the N+2 HttpWebRequest.GetRequestStream() works again. It's like the N+2 or my router remembers the last IPs being used and don't allow traffic to go thru, either that or COSM is rejecting it and making it hang, which I doubt.

 

I already set a timeout for the HttpWebRequest.GetRequestStream but doesn't help at all, it just freezes.

 

So, what could be happening? Is my router not allowing the traffic to go thru?

 

Attached is the code I am using to push code to COSM.

 

Attached Files



#2 emg

emg

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 25 February 2013 - 10:15 AM

What's happening to the MAC address every time it's freezing?



#3 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 25 February 2013 - 11:53 AM

I set up a test feed on Saturday and I'm using sockets instead.  Give this a try and see if it works any differently.

 

You instantiate it using:

CosmClient cc = new CosmClient();

 

Then send it using (build your CSV string as necessary):

cc.Send("TEMP," + "73" + "rn");

 

Here's the class:

using System.Net;using System.Net.Sockets;using System.Text;using System;using Microsoft.SPOT;namespace ScareTronix.PandorasBox.Services{    public class CosmClient    {        const string APIKEY = ""; // your cosm api key        const string FEEDID = ""; // your feed ID        const string USERAGENT = ""; // user agent is the project name (ie: "my project name (feedid)")        static Socket serversocket = null;        static IPEndPoint remoteEndPoint = null;        public CosmClient()        {            IPHostEntry entry = Dns.GetHostEntry("api.cosm.com");            IPAddress address = entry.AddressList[0];            remoteEndPoint = new IPEndPoint(address, 80);        }        public void Send(string CSVdata)        {            try            {                serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                serversocket.Connect(remoteEndPoint);                // calculate the length of the sensor reading in bytes:                // 8 bytes for "sensor1," + number of digits of the data:                int ContentLength = CSVdata.Length;                string WholePacket = "";                // send the HTTP PUT request:                WholePacket += "PUT /v2/feeds/" + FEEDID.ToString() + ".csv HTTP/1.1rn";                WholePacket += "Host: api.cosm.com" + "rn";                WholePacket += "X-ApiKey: " + APIKEY + "rn";                WholePacket += "User-Agent: " + USERAGENT + "rn";                WholePacket += "Content-Length: " + ContentLength.ToString() + "rn";                // last pieces of the HTTP PUT request:                WholePacket += "Content-Type: text/csv" + "rn";                WholePacket += "Connection: close" + "rn";                WholePacket += "rn";                // here's the actual content of the PUT request:                WholePacket += CSVdata + "rn";                byte[] buffer = Encoding.UTF8.GetBytes(WholePacket);                serversocket.Send(buffer);                bool poll = serversocket.Poll(1000000, SelectMode.SelectRead);                if (serversocket.Available > 0)                {                    buffer = new byte[serversocket.Available];                    serversocket.Receive(buffer);                    string resp = new string(Encoding.UTF8.GetChars(buffer));                    Debug.Print(resp);                }                serversocket.Close();            }            catch (Exception ex)            {                Debug.Print(ex.Message);            }        }    }} 


#4 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 26 February 2013 - 07:32 AM

Hi emg,

 

Mac Address remains the same.



#5 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 26 February 2013 - 07:46 AM

Hi Dave,

 

Thanks for providing your class, I really like your approach.

 

The program works fine the first time around, if I stop the debugging or restart the N+2 without debugging, either way, then the the program hangs on the line of code below:

 

serversocket.Connect(remoteEndPoint);

 

Then the only solution to get it to work again is to go to MFDeploy and change the N+2 Static IP address to something else available in the network, then starts working once again but I stop and re-run, then it freezes on the same line of code.

 

Any clues what is going on? I am suspecting that my router is doing something to block the traffic but can't confirm and if that the case then the program should timeout. Should I use sniffer or fiddler as proxy and see if the traffic is going thru?



#6 dbradshaw

dbradshaw

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationUK

Posted 26 February 2013 - 12:01 PM

I have found a similar issue:

 

Like to OP I think that if my code runs to completion then the following execution is OK; however when I terminate my code or it crashes then subsequent executions may hang on the following line.

 

Response = request.GetResponse();
 

Setting request.ReadWriteTimeout seems to improve this dramatically. The default is 300000 which (assuming mS) is 5 minutes.

 

Try the following experiment

  • Note the time
  • Start Code – terminate after GetResponse is called but before a normal complete
  • Start code a second time – check it hangs on GetResponse
  • Wait until 5 minutes have elapsed from the time in 1)
  • Start code a third time to see if it hangs

I'm wondering if because the previous request hasn't yet been completed (disposed, closed etc.) then subsequent requests get queued and you are waiting for the first one to complete or timeout.

 

I changed my code to set the ReadWriteTimeout to 2000 (not sure if this is too aggressive)

try{   // Create an HTTP Web request.   HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;   request.ReadWriteTimeout = 2000;   request.KeepAlive = false;   try   {       Utils.WriteToDebugWindow("InternetStreamReader", "Executing request.GetResponse");       Response = request.GetResponse();       Utils.WriteToDebugWindow("InternetStreamReader", "Executed request.GetResponse");   }   catch (Exception e)   {       Utils.WriteToDebugWindow("InternetStreamReader", "Exception in request.GetResponse" + e.ToString());   }}catch (Exception ex){}

 

Dave



 

 



 

 



 



#7 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 26 February 2013 - 12:30 PM

I would give dbradshaw's (Dave) suggestion a try.  It could be a timeout issue but could be caused by hardware (router, Modem).  Timeout could be a quick fix.
 



#8 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 26 February 2013 - 09:16 PM

Interesting and thanks to both for their replies. I've been playing with the timeouts for all the parameters but they didn't help I remember but since I am getting old and I don't remember things that I've done lately I am going to try dbradshaw suggestion and I'll keep you both posted.

 

Thank you so much



#9 dbradshaw

dbradshaw

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationUK

Posted 26 February 2013 - 09:51 PM

Yeah - don't get your hopes up - mine has started playing up again :angry:



#10 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 February 2013 - 02:46 AM

Hi Giuliano, With the static IP... If you reboot your Netduino Plus 2 and it still can't connect (without changing the IP address), then it's most likely a router issue. Try restarting the router...and see if the issue clears up. Also...do you have the static IP reserved for your Netduino Plus 2's MAC address on the router? There are DDoS protections built into some routers...so it's possible that the traffic is triggering an automatic port block. Chris

#11 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 27 February 2013 - 05:22 AM

Hi dbradshaw,

 

I tried your code and it hangs on the second call in this line of code:

 

Response = request.GetResponse();

 

I can let it run for 15 minutes or even more but the program sits on that line of code. Then if I change the Static IP addres on the N+2 to something else other than previous used IP that line of code works until the program is stop. A re-run with the same Static IP address wil cause the program to hang on the same line of code once again.



#12 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 27 February 2013 - 05:34 AM   Best Answer

Hi Chris,

 

Thank you very much for you reply. You nailed it, it was the MAC Address being reserved in the router (see snapshot below), even thoght I was changing the Static IP address on the N+2 using MFDeploy Network Configuration to something else than 3.2.72.20, seems like the router was blocking the traffic the second time around coming from the same MAC address withing a different IP address.

 

Posted Image

 

Man, I spent so much time on this issue, what a relief!!!

 

dbradshaw, maybe you have the same issue I had.

 

Cheers!



#13 emg

emg

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 27 February 2013 - 10:07 AM

Wow, that's strange. What type of router do you have? What your saying is that effectively you can use every IP address only once until router reboot? I assume this is the ARP cache and it's supposed to 're-register' a device every time it connects and periodically, see here ARP Cache - What is it?

 

So is this a result of no/bad broadcast support in the lwip stack (i.e., not registering as dynamic) or something else? If you take one of your other connected devices that are using a fixed IP address (Apple TV or Samsung printer in your example) does it also have the same problem?



#14 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 27 February 2013 - 03:56 PM

Hi emg, I have an old Dlink DIR-655. I know it is time for me to upgrade but this router has been working rock solid for over years and I hate to spend the money when it is not really needed. That said, I will conduct some testing as you suggested and I'll keep you posted of my findings.

 

Thanks



#15 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 27 February 2013 - 08:37 PM

Hi emg,

 

I tried to reserved the MAC addresses of most of my devices at home (even though my router only allows to reserve 25 MAC addresses at the most), so they always get the same IP address assigned, that way I know for sure they will remain in that IP address and I can do port forwarding and those kind of things. I do not go to every device and set an static IP address for them, the router takes care of that but in the N+2 situation, you have to either use DHCP or choose an IP address.

 

in order to be able to conduct a test with one of the other devices in my network, I would have to go to their settings and change it to use a Static IP Addres, e.g.: Change Apple TV in 3.2.72.23 to have static IP address as 3.2.72.105 for example without changing the reserverd MAC Address in the router which will still be pointing to 3.2.72.23, then see if it works after booting it up a few times.

 

As I am writing this I realize I can leave the MAC Address in the router reserverd MAC Addresses and just select DHCP in the N+2 Network Configuration, that way the N+2 will always get the 3.2.72.20. So I am going to try this later and I'll post the results here.

 

Thank you



#16 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 01 March 2013 - 06:16 AM

Quick update:

 

I went back to my router and reserved the MAC address for my N+2 to always be adquired in ip address 3.2.72.20 and then I changed its Network Configuration using MFDeploy to be DHCP (checkbox checked) and no problems anymore, works like a charm. So either this way or by deleting it from the router reserved MAC address and then using a Static IP in the N+2 as 3.2.72.20 works just fine.



#17 emg

emg

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 01 March 2013 - 09:35 AM

Thanks, for the update. That looks like the better option for you (set device to DHCP and reserve it at the DHCP server). It is still unusual that the netduino that changes its static IP address does not signal the change to your router and you have to manually clear the ARP cache. This should happen by OSI model design.






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.