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

Intermittent network error. Socket gets dropped


  • Please log in to reply
2 replies to this topic

#1 SteveS123

SteveS123

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationMissouri

Posted 04 November 2013 - 11:04 PM

I have been struggling with the ND+ and sockets.  I used the code from http://www.element14...uino-web-server to setup my ND+ to listen for requests on a socket and to change LEDs sequences depending on the request.  Without a lot of detail I would like to tell you the problem first since it may save a lot of reading.

The ND+ listens on a socket and works sometimes but it will always fail eventually or even right away.

[color=#ff0000;]The error I receive is "No connection could be made because the target machine actively refused it".[/color]

 

 I have used MFDeploy to setup a static IP and I even used a line in code to set the IP statically but neither seem to matter.

Like I said, sometimes it works and sometimes it doesn't.  I've been racking my brain for days.  Is the problem because I am listening on one thread and processing the result on another?  

I can ping the device with windows command prompt indefinitely while sending requests.  I can even ping the device after the network error shows up but the application on the ND+ is locked up.

 

The two files attached are:

1. The application that is on the ND+.

2. The dll that is used to talk to the ND+.

 

To use it just create a forms app and import the dll and you should be able run the program without any peripherals connected to the ND+ except power and a network cable.

 

Thank you in advance for any helpful information.

Steve

 

 public static void Main()
        {
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP("10.0.0.206", "255.255.255.0", "10.0.0.1");
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Any, 8080));
            socket.Listen(10);
 
            //thread start
            ThreadStart threadStart = new ThreadStart(workerThread);
            Thread threadWorker = new Thread(threadStart);
            threadWorker.Start();
 
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
           
                    while (true)
                    {
                        string rawData = "";
                        using (Socket commSocket = socket.Accept())
                        {
                            while (!commSocket.Poll(10, SelectMode.SelectRead)) ;
                            led.Write(true);
                            int bytesSent = commSocket.Available;
                         
                            if (bytesSent > 0)
                            {
                                byte[] bytes = new byte[bytesSent];
                                int bytesReceived = commSocket.Receive(bytes, bytesSent, SocketFlags.None);
                                if (bytesReceived == bytesSent)
                                {
                                    rawData = (new string(Encoding.UTF8.GetChars(bytes)));
                                }
                            }
 
                            if (rawData == "PhotoOn")
                            {
                                sequence = "DMXPhotoOn";
                                break;
                            }
 
                            else if (rawData == "PhotOff")
                            {
                                sequence = "DMXPhotoOff";
                                break;
                            }
 
                            //start of new LEDController code to seperate passed parameters
                            else if (rawData != "PhotoOff" || rawData != "PhotoOn")
                            {
                                char delimiter = ' ';
                                string[] words;
                                words = rawData.Split(delimiter);
                                sequence = words[0];
                                red = byte.Parse(words[1]);
                                green = byte.Parse(words[2]);
                                blue = byte.Parse(words[3]);
                                LEDChain = words[4];
                            }
                            led.Write(false);
                        }
                    }
        }

Attached Files



#2 NoxiaZ

NoxiaZ

    Member

  • Members
  • PipPip
  • 12 posts

Posted 06 November 2013 - 12:33 PM

You got no error handling in your while loop, so if you get any exception the loop will exit.

Use

try

{

...

}

catch { }



#3 SteveS123

SteveS123

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationMissouri

Posted 06 November 2013 - 05:29 PM

I may be wrong but I'm guessing the ND+ should stay open for network traffic and listen on the socket.  I can flash the ND+ and try to send it a packet over the network and I will get an error that says, "no connection could be made because the target machine actively refused it".  I have to reboot the ND+ and then maybe it will accept the packet.  Then sometimes it will work for a while (hours) and then throw an error again.

 

Could the problem be because I'm multithreading?  One thread is listening for packets while the other is telling LEDs what to do through SPI.






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.