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

Ethernet drops connection randomly


  • Please log in to reply
3 replies to this topic

#1 SteveS123

SteveS123

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationMissouri

Posted 09 March 2014 - 10:03 PM

I am connecting 10 ND+2 together and they all communicate with each other via Enet.  I have all of them set static.  My problem is that at random times I can't communicate with any one of the ND+2's.  The only way I can get the device to communicate is to reset the controller by unplugging it.  

 

Is there a way to prevent this?

Is there a way to open a socket connection and try to send to it but if the connection isn't available then just ignore the connection?  (I tried putting a "try-catch" in the code but the controller still hangs if there is no endpoint)

 

 

 

I'm new to sockets so if you know of a better way to do this I would greatly appreciate the advice.

 

Here is the way I'm doing the connections:

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)));
                        }
                    }
                    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];
                            //speednMS = int.Parse(words[5]);
                            led.Write(false);
 
 try
                                {
                                    using (Socket sendSocket = new Socket(AddressFamily.InterNetwork,                    SocketType.Stream, ProtocolType.Tcp))
                                    {
                                        sendSocket.SendTimeout = 1000;
                                        sendSocket.Connect(EPCanIP);
                                        sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
                                        sendSocket.Close();
                                    }
                                    using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                                    {
                                        sendSocket.SendTimeout = 1000;
                                        sendSocket.Connect(EPFloorIP);
                                        sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
                                        sendSocket.Close();
                                    }
                                    using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                                    {
                                        sendSocket.SendTimeout = 1000;
                                        sendSocket.Connect(EPRibsIP);
                                        sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
                                        sendSocket.Close();
                                    }
                                    using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                                    {
                                        sendSocket.SendTimeout = 1000;
                                        sendSocket.Connect(EPRevealIP);
                                        sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
                                        sendSocket.Close();
                                    }
 
                                    using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                                    {
                                        sendSocket.SendTimeout = 1000;
                                        sendSocket.Connect(EPGreenScreenIP);
                                        sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
                                        sendSocket.Close();
                                    }
 
                                    using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                                    {
                                        sendSocket.SendTimeout = 1000;
                                        sendSocket.Connect(EPWelcomeIP);
                                        sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
                                        sendSocket.Close();
                                    }
                                }
                                catch (Exception)
                                {
 
                                    Debug.Print("Socket did not connect");
                                }


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 April 2014 - 09:44 PM

Hi Steve,

Did the socket unblocking tip from this thread resolve this issue for you?

Chris

#3 SteveS123

SteveS123

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationMissouri

Posted 23 April 2014 - 05:28 PM

Chris,

 

The "unblocking" solution did solve the problem when one of the ND's dropped connection.  The main ND that talks to each of the others just ignores that connection now and continues but I still have the problem where the ND's are just dropping connection and random times.

 

I'm now wondering if I'm using my "using" blocks wrong.  Each controller opens a socket and listens for requests.  The using statement is executed every time through the while loop.  

Question 1:  Should I open a socket that listens and place it before the while loop and just keep it open?

Question 2:  Should I not use the "using" each time I want to communicate to a ND?  I.E. I need to send an LED sequence to all 6 ND's then I am using 6 "using" and closing each connection in each using.  What I'm wondering is do I just open 6 sockets in my program and leave them open?

 

I hope this makes sense.

 

Steve



#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 23 April 2014 - 08:28 PM

Hi Steve,

I'd try just leaving the sockets open, and seeing how that changes the behavior of your app.

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.