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

No Exception on lost Ethernet connection


  • Please log in to reply
3 replies to this topic

#1 Cinelli

Cinelli

    Member

  • Members
  • PipPip
  • 17 posts

Posted 09 August 2011 - 05:48 AM

This code works fine while Ethernet is plugged in. It also properly throws exceptions and retries if you start with Ethernet disconnected. If you plug in after some failures it also restarts correctly. The problem comes if you boot with Ethernet connected, let it run for 1 or more loops, then disconnect (or lose) Ethernet. In this last case GetRequestStream neither returns nor throws an exception: the device locks up.

Any insights? Thanks in advance.

[size="4"]
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.SPOT;

namespace M2W.NetduinoReader
{
    class WebTest
    {
        public static void Main()
        {
            for(int i = 0; i < 20; i++)
            {
                Thread.Sleep(3000);

                using(HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://www.google.com"))
                {
                    request.Timeout = 15000;
                    request.ReadWriteTimeout = 5000;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    byte[] bytes = Encoding.UTF8.GetBytes("a=3&b=4");
                    request.ContentLength = bytes.Length;
                    Debug.Print("request.GetRequestStream");
                    try
                    {
                        using(Stream requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(bytes, 0, bytes.Length);
                            requestStream.Close();
                        }
                    }
                    catch(WebException ex)
                    {
                        Debug.Print("WebException on GetRequestStream: " + ex.Message);
                        continue;
                    }
                    catch(Exception ex)
                    {
                        Debug.Print("Exception on GetRequestStream: " + ex.Message);
                        continue;
                    }

                    Debug.Print("request.GetResponse");
                    try
                    {
                        using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                        {
                            int respSize = System.Math.Min((int)response.ContentLength, 80);
                            if(respSize != 0)
                            {
                                byte[] respData = new byte[respSize];
                                try
                                {
                                    Debug.Print("request.GetResponseStream");
                                    using(Stream responseStream = response.GetResponseStream())
                                    {
                                        responseStream.Read(respData, 0, respData.Length);
                                        responseStream.Close();
                                    }
                                    string responseString = new string(Encoding.UTF8.GetChars(respData));
                                    Debug.Print("Response was: " + responseString);
                                }
                                catch(WebException ex)
                                {
                                    Debug.Print("WebException on GetResponseStream: " + ex.Message);
                                }
                                catch(Exception ex)
                                {
                                    Debug.Print("Exception on GetResponseStream: " + ex.Message);
                                }
                            }
                            else
                            {
                                Debug.Print("Response was empty");
                            }
                            response.Close();
                        }
                    }
                    catch(WebException ex)
                    {
                        Debug.Print("WebException on GetResponse: " + ex.Message);
                    }
                    catch(Exception ex)
                    {
                        Debug.Print("Exception on GetResponse: " + ex.Message);
                    }
                }
            }
        }
    }
}
[/size]


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 09 August 2011 - 05:56 AM

Hi Cinelli, Have you tried the new .NET MF 4.2 Release Candidate 1 firmware? A lot of Ethernet bugs were fixed in this new release--and this may be one of them :) If not, we should get a bug report in now (at netmf.codeplex.com) so that the team can look at it. .NET MF 4.2 is set to be officially released any week now. Chris

#3 Cinelli

Cinelli

    Member

  • Members
  • PipPip
  • 17 posts

Posted 09 August 2011 - 06:13 AM

Yes we are on the Sunday night 4.2 RC. Will do on the bug. BTW is Connect used for NetMF also? It was back in 2.5, though I wonder if Open Source killed that approach.

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 09 August 2011 - 06:33 AM

Hi Cinelli, Connect is no longer used for NETMF. We can all now get regular code updates through netmf.codeplex.com -- which is even more awesome :) Chris




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.