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

The occasional Network Error

NP2 networking

  • Please log in to reply
8 replies to this topic

#1 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 18 April 2013 - 11:15 PM

Hi All,

 

I'm running the basic HelloWorld webserver example with an NP2 connected to my campus network. It seems to work about half the time, with the other half I'm getting "No Data Received" from my browser, and "The underlying connection was closed: The connection was closed unexpectedly" exception from a little forms application I wrote for my computer.

 

Is this a timing issue?



#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 19 April 2013 - 07:42 AM

You probably need to put a try/catch around your incoming request and handle the exception gracefully. Also, "GET /favicon.ico HTTP/1.1" will cause problems unless you account for it. It seems that the IE browser will sometimes send this.  Ref: http://www.avajava.c...i-have-one.html

 

I just kill it like this,
 

If (Not Request.IndexOf("favicon.ico") = -1) Then   Debug.Print("received favicon.ico")   Request = ""End IfIf (Request.Length > 0) Then ..

Baxter



#3 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 24 April 2013 - 09:50 PM

You probably need to put a try/catch around your incoming request and handle the exception gracefully. Also, "GET /favicon.ico HTTP/1.1" will cause problems unless you account for it. It seems that the IE browser will sometimes send this.  Ref: http://www.avajava.c...i-have-one.html

 

I just kill it like this,
 

If (Not Request.IndexOf("favicon.ico") = -1) Then   Debug.Print("received favicon.ico")   Request = ""End IfIf (Request.Length > 0) Then ..

Baxter

I don't think that's the problem. I put try/catch around the appropriate code on the PC side, but it still only connects to the netduino maybe 5-50% of the time, I guess depending on the day. It's kind of a huge road block. Could this just be an annoying feature of an enterprise network?

 

 

Anyway, I implemented the check for favicon.ico, but I write in C#. Did I get the syntax right?

using (Socket clientSocket = socket.Accept())                {                    //Get clients IP                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;                    EndPoint clientEndPoint = clientSocket.RemoteEndPoint;                    //int byteCount = cSocket.Available;                    int bytesReceived = clientSocket.Available;                    if (bytesReceived > 0)                    {                        //Get request                        //TimeSpan hoy = DateTime.TimeOfDay;                        byte[] buffer = new byte[bytesReceived];                        int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);                        string request = new string(Encoding.UTF8.GetChars(buffer));                        if (!(request.IndexOf("favicon.ico") == -1))                        {                            Debug.Print("received favicon.ico");                            request = "";                        }                        if (request.Length > 0)                        {                            Debug.Print(request);                            LCD.Position(0, 0);                            LCD.Print(DateTime.Now.TimeOfDay.ToString());                            //Compose a response                            string response = "The time is: " + System.DateTime.Now.ToString() + ". Hello World!!!nThis response was sent due to a request from " + clientIP.ToString() + ".nPlease wait a few seconds before trying to connect again.";                            string header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn";                            clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);                            clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);                            //Blink the onboard LED                            led.Write(true);                            Thread.Sleep(1000);                            led.Write(false);                        }                    }                }


#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 24 April 2013 - 10:10 PM

Hi Verdris, Have you written out any debugging info from the NP2? Is it seeing the requests? If so, sniffing the data using WireShark may help narrow down where the traffic flow is stopping. Chris

#5 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 24 April 2013 - 10:24 PM

Hi Verdris, Have you written out any debugging info from the NP2? Is it seeing the requests? If so, sniffing the data using WireShark may help narrow down where the traffic flow is stopping. Chris

 

I just solved the problem. I think the issue was that the Netduino was trying to process the socket request too quickly. It kept returning 0 bytes received. I added a short delay at the beginning of the socket.Accept() block and haven't had an error since. Code is as follows:

 

        public void ListenForRequest()        {            while (true)            {                using (Socket clientSocket = socket.Accept())                {                    socketConnectLED.Write(true);                    Thread.Sleep(10);                    //Get client's IP                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;                    EndPoint clientEndPoint = clientSocket.RemoteEndPoint;                    //int byteCount = cSocket.Available;                    int bytesReceived = clientSocket.Available;                    Debug.Print("Client IP: " + clientIP.ToString());                    Debug.Print("Client Endpoint: " + clientEndPoint.ToString());                    Debug.Print("Bytes received: " + bytesReceived.ToString());                    if (bytesReceived > 0)                    {                        //Get request                        byte[] buffer = new byte[bytesReceived];                        int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);                        string request = new string(Encoding.UTF8.GetChars(buffer));                        if (!(request.IndexOf("favicon.ico") == -1))                        {                            Debug.Print("received favicon.ico");                            request = "";                        }                        if (request.Length > 0)                        {                            Debug.Print(request);                            LCD.Position(0, 0);                            LCD.Print(DateTime.Now.TimeOfDay.ToString());                            //Compose a response                            string response = "The time is: " + System.DateTime.Now.ToString() + ". Hello World!!!nThis response was sent due to a request from " + clientIP.ToString() + ".nPlease wait a few seconds before trying to connect again.";                            string header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn";                            clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);                            clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);                            //Blink the onboard LED                            led.Write(true);                            Thread.Sleep(250);                            led.Write(false);                        }                    }                    socketConnectLED.Write(false);                }            }        }


#6 Shadi

Shadi

    Member

  • Members
  • PipPip
  • 12 posts

Posted 25 April 2013 - 10:16 AM

just like your serialport issue, you cannot ignore the return value of receive().

so you would have to fill a buffer in a similar fashion until its full or receive returns 0.

then you can convert the total amount received (not the entire buffer) to a string and parse it.



#7 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 25 April 2013 - 11:15 PM

just like your serialport issue, you cannot ignore the return value of receive().

so you would have to fill a buffer in a similar fashion until its full or receive returns 0.

then you can convert the total amount received (not the entire buffer) to a string and parse it.

I'm not calling any receive() methods. All I have is int bytesReceived = clientSocket.Available; which just tells me that there's some kind of request. That's essentially all I care about, since I'm running the Netduino example as an instrumentation monitor that just needs to pump out data when requested.



#8 Shadi

Shadi

    Member

  • Members
  • PipPip
  • 12 posts

Posted 26 April 2013 - 12:44 AM

I must be seeing things...

int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);string request = new string(Encoding.UTF8.GetChars(buffer)); 


#9 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 26 April 2013 - 06:26 AM

I must be seeing things...

int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);string request = new string(Encoding.UTF8.GetChars(buffer)); 

lol@me. I wasn't seeing that.

 

The program is working flawlessly now, though, without any peeking through the buffer until it's done.







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.