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

TCP Network communication very slow


  • Please log in to reply
5 replies to this topic

#1 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 18 September 2011 - 08:35 AM

Hi everybody!
I'm new in this forum and I've bought my first NETduino plus a few weeks ago (I have a little experience with c# and other uCPU but I'm totally newbie with Arduino/NETduino).
I' writing 2 projects:
1) a server (on NETduino) waiting for a TCP connection and able to manage 2 simple commands: LED ON and LED OFF.
2) a client (on my PC) to establish a TCP connection with the server and able to send the same 2 commands.

This is the code on the server:
public void Listerer()
        {
            bool Loop = true;
            bool RxLoop = true;

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(new IPEndPoint(IPAddress.Any, mConfig.Port));

                Debug.Print("Waiting connection...");

                socket.Listen(5);
                while (Loop == true)
                {
                    using (Socket commSocket = socket.Accept())
                    {
                        RxLoop = true;
                        while (RxLoop == true)
                        {
                            if (commSocket.Poll(1, SelectMode.SelectError) == true)
                            {
                                Debug.Print("SOCKET ERROR DETECTED");
                            }

                            if (commSocket.Poll(1, SelectMode.SelectRead) == true)
                            {
                                if (commSocket.Available == 0)
                                {
                                    // Disconnected!
                                    Debug.Print("Disconnected");
                                    RxLoop = false;
                                }
                                else
                                {
                                    int BuffSize = commSocket.Available;
                                    byte[] bytes = new byte[BuffSize];
                                    int count = commSocket.Receive(bytes);

                                    if (count > BuffSize)
                                    {
                                        Debug.Print("ERROR! : Buffer overflow");
                                        PowerState.RebootDevice(false);
                                    }
                                    if (count == 0)
                                    {
                                        Debug.Print("ERROR! : Rx empty string");
                                        continue;
                                    }
                                    string s = new String(Encoding.UTF8.GetChars(bytes));

[here I clean the RX buffer: trim, remove <space>, <newline>, etc etc...]

                                    if (s == "LEDON")
                                    {
[Onboard LED ON]

                                        commSocket.Send(StrToByteArray("OK" + NewLine));
                                    }
                                    else if (s == "LEDOFF")
                                    {
[Onboard LED OFF]

                                        commSocket.Send(StrToByteArray("OK" + NewLine));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

and here the code for the client:
        client = new TcpClient(sIPAddress, iPort);
        stream = client.GetStream();

        do {

[TX]        bBuffer = System.Text.Encoding.ASCII.GetBytes("LEDON"); 
            stream.Write(bBuffer, 0, bBuffer.Length);
            stream.Flush();

[RX]        Int32 bytes = stream.Read(bBuffer, 0, bBuffer.Length);

[TX]        bBuffer = System.Text.Encoding.ASCII.GetBytes("LEDOFF"); 
            stream.Write(bBuffer, 0, bBuffer.Length);
            stream.Flush();

[RX]        Int32 bytes = stream.Read(bBuffer, 0, bBuffer.Length);

        } while(1);

I was expecting to see the LED flashing very fast but actually it flashes once per second! Why so slow?

Here what is going on the LAN (thanks Wireshark!):
No.     Time        Source                Destination           Protocol Length Info
      7 0.225345    192.168.1.141         192.168.1.21          TCP      54     57189 > http [ACK] Seq=9 Ack=9 Win=65384 Len=0
      8 0.503487    192.168.1.141         192.168.1.21          HTTP     63     Continuation or non-HTTP traffic
      9 0.515869    192.168.1.21          192.168.1.141         TCP      60     http > 57189 [ACK] Seq=9 Ack=18 Win=1007 Len=0
     10 0.518783    192.168.1.21          192.168.1.141         HTTP     63     Continuation or non-HTTP traffic
     11 0.725376    192.168.1.141         192.168.1.21          TCP      54     57189 > http [ACK] Seq=18 Ack=18 Win=65375Len=0
     12 1.005504    192.168.1.141         192.168.1.21          HTTP     62     Continuation or non-HTTP traffic
     13 1.009187    192.168.1.21          192.168.1.141         TCP      60     http > 57189 [ACK] Seq=18 Ack=26 Win=999 Len=0
     14 1.011983    192.168.1.21          192.168.1.141         HTTP     62     Continuation or non-HTTP traffic

192.168.1.141: PC
192.168.1. 21: NETduino

Replay is very fast but seems each command is split in 2 part and it took about 300ms between the 1st and 2nd part.

I have attached the whole projects (there is a Scan function to search all NETduino boards on the network, maybe someone could be interested in it...).
Please help!

Rusko

Attached Files


--
Rusko

#2 Bill Cook

Bill Cook

    New Member

  • Members
  • Pip
  • 5 posts

Posted 18 September 2011 - 09:59 PM

I added a timing metric to your code and I'm averaging 10 "LEDON" commands every 400ms, or 1 "LEDON" command every 40ms. It could be something to do with your LAN. Try wrapping areas of the client and server code to figure out where the bottle neck is. I see some places where you could increase performance, but nothing that would have a significant improvement.
Note: I didn't download your entire solution, I just copy/pasted the snippets you provided, so it is possible the performance bottle neck is in another area that I didn't use.
Here is what I'm running on the Netduino:
        public static void Listerer()
        {
            bool Loop = true;
            bool RxLoop = true;

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(new IPEndPoint(IPAddress.Any, 1234));

                Debug.Print("Waiting connection...");

                socket.Listen(5);

                int LEDOnCount = 0;
                DateTime referenceTime = DateTime.Now;
                while (Loop == true)
                {
                    using (Socket commSocket = socket.Accept())
                    {
                        RxLoop = true;
                        while (RxLoop == true)
                        {
                            if (commSocket.Poll(1, SelectMode.SelectError) == true)
                            {
                                Debug.Print("SOCKET ERROR DETECTED");
                            }

                            if (commSocket.Poll(1, SelectMode.SelectRead) == true)
                            {
                                if (commSocket.Available == 0)
                                {
                                    // Disconnected!
                                    Debug.Print("Disconnected");
                                    RxLoop = false;
                                }
                                else
                                {
                                    int BuffSize = commSocket.Available;
                                    byte[] bytes = new byte[BuffSize];
                                    int count = commSocket.Receive(bytes);

                                    if (count > BuffSize)
                                    {
                                        Debug.Print("ERROR! : Buffer overflow");
                                        PowerState.RebootDevice(false);
                                    }
                                    if (count == 0)
                                    {
                                        Debug.Print("ERROR! : Rx empty string");
                                        continue;
                                    }
                                    string s = new String(System.Text.Encoding.UTF8.GetChars(bytes));

//[here I clean the RX buffer: trim, remove <space>, <newline>, etc etc...]

                                    if (s == "LEDON")
                                    {
//[Onboard LED ON]

                                        commSocket.Send(StrToByteArray("OK" + "\n"));
                                        led.Write(true);
                                        LEDOnCount++;

                                        if (LEDOnCount == 10)
                                        {
                                            Debug.Print((DateTime.Now - referenceTime).Milliseconds.ToString());
                                            referenceTime = DateTime.Now;
                                            LEDOnCount = 0;
                                        }
                                    }
                                    else if (s == "LEDOFF")
                                    {
                                        commSocket.Send(StrToByteArray("OK" + "\n"));
                                        led.Write(false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        public static byte[] StrToByteArray(string theString)
        {
            return System.Text.Encoding.UTF8.GetBytes(theString);
        }


#3 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 19 September 2011 - 03:51 AM

I ran your code and the led blinks so rapidly that it appears to be on almost continuously. I use a static IP address, but I notice from MFdeploy that the network configuration was set back to DHCP after running yor code. My Netduino is plugged into a WD Livewire powerline adapter port which is connected to my router. Your router is probably causing your problem.

Output from several connects,
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Gateway Address: 192.168.0.1
IP Address: 192.168.0.106
Subnet mask 255.255.255.0
Waiting connection...
Disconnected
done
Disconnected
done
Wait Handler OK!
The thread '<No Name>' (0x1) has exited with code 0 (0x0).
Disconnected
done
Disconnected
done
Baxter

#4 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 19 September 2011 - 07:43 AM

Thank you very much for your answers! I made some tests and the problem seems to be the port I was using. I changed the port (from 80 to 437) and now is very fast (I didn't measure it but I think is similar to the measure Bill Cock made). Now I'm trying to understand why port 80 is so slow. I don't think is the router (I use a WRT54GL with fw Tomato and QoS: port 80 has higher priority than 437...), probably is the lwIP Network stack or some services on my PC... what do you think? Note: I forgot to mention it, I'm using the Firmware 4.2.0 RC1. Thank you again!
--
Rusko

#5 Bill Cook

Bill Cook

    New Member

  • Members
  • Pip
  • 5 posts

Posted 19 September 2011 - 02:13 PM

There could be a firewall that is causing the delay. See if adding your client app to the exception list helps. Also, see if there are any port 80 exceptions.

#6 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 20 September 2011 - 07:19 AM

Boh... the firewall seems OK. I will try with a different PC... Thanks again!
--
Rusko




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.