0

I have this piece of code in my program that sometimes, like once a week, keeps exiting with this excpetion, but I really can't understend which part of the code is wrong. Can someone analyze it and help me figure this out? I have to do a "power cycle" of the program to make it stop.

Here is the code:


Consider that multicast and radiomode are always false.

public void ComThread()
{
         byte[] buffer, data, remotebytes, recvbytes, attatched;
         int port;
         uint portindex;
         string a, b, agvno;
         string debugstring;
         int agvindex = 0;
         System.Net.IPEndPoint recvendpoint;
         System.Net.EndPoint ep;
         mw.Log(MhcsLib.DebugZones.StartupShutdown,  DEVCOM_UDP_RECV_STARTING);
               recvendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
               ep = (System.Net.EndPoint)(recvendpoint);
               remotebytes = remoteendpoint.Address.GetAddressBytes();
               attatched = new byte[44];

        while (true)
        {
            try
            {
                recvendpoint.Address = System.Net.IPAddress.Any;
                if (multicast)
                {
                    buffer = multicastclient.Receive(ref recvendpoint);
                }
                else
                {
                    // start listening from ANY IP address and put in buffer the result if there is any
                    buffer = UdpClient.Receive(ref recvendpoint);

                    debugstring = ByteArrayToString(buffer);
                    //mw.Log(MhcsLib.DebugZones.Events, AGVCTL_LOG, debugstring);
                    //mw.Log(MhcsLib.DebugZones.Events,)
                }
                recvbytes = recvendpoint.Address.GetAddressBytes(); // get the bytes of the source IP address
                port = recvendpoint.Port; // get the port of the source IP
                if (buffer.Length == 32)
                {                       
                    Array.Copy(buffer, 0, attatched, 0, buffer.Length);
                }
                else if (buffer.Length == 12 && attatched[0] != 0)
                {
                    Array.Copy(buffer, 0, attatched, 32, buffer.Length);
                }
                if (radiomode && buffer.Length != 4)
                {
                    if (buffer.Length > 6 && buffer.Length != 32 & attatched[43] == 0)
                    {
                        // get the AGV ID from the response polling message of the AGV in agvindex
                        a = System.Text.Encoding.ASCII.GetString(new byte[] { buffer[4] });
                        b = System.Text.Encoding.ASCII.GetString(new byte[] { buffer[5] });
                        agvno = a + b;
                        agvindex = Int32.Parse(agvno, System.Globalization.NumberStyles.HexNumber);
                        // if system in radiomode then call the message received method in AgvCtl with the index from the polling
                        portindex = (uint)agvindex - 1;
                        data = new byte[buffer.Length];
                        Array.Copy(buffer, 0, data, 0, buffer.Length);
                        if (portindex < nagv && portindex >= 0) // check if the agvindex is compatible
                        {
                            MessageReceived(portindex, data); // portindex is used to chose witch VCB record it should be opened
                        }
                    } 
                    else if (buffer.Length == 12 && attatched[0] != 0)
                    {
                        // get the AGV ID from the response polling message of the AGV in agvindex
                        a = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[4] });
                        b = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[5] });
                        agvno = a + b;
                        agvindex = Int32.Parse(agvno, System.Globalization.NumberStyles.HexNumber);
                        // if system in radiomode then call the message received method in AgvCtl with the index from the polling
                        portindex = (uint)agvindex - 1;
                        data = new byte[attatched.Length];
                        Array.Copy(attatched, 0, data, 0, attatched.Length);
                        MessageReceived(portindex, data);
                        Array.Clear(attatched, 0, attatched.Length);
                    }
                }
                else
                {
                    if (buffer.Length == 12 && attatched[0] != 0)
                    {
                        // get the AGV ID from the response polling message of the AGV in agvindex
                        a = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[4] });
                        b = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[5] });
                        agvno = a + b;
                        agvindex = Int32.Parse(agvno, System.Globalization.NumberStyles.HexNumber);
                        // if system in radiomode then call the message received method in AgvCtl with the index from the polling
                        portindex = (uint)agvindex - 1;
                        data = new byte[attatched.Length];
                        Array.Copy(attatched, 0, data, 0, attatched.Length);
                        MessageReceived(portindex, data); // portindex is used to chose witch VCB record it should be opened
                        Array.Clear(attatched, 0, attatched.Length);
                    }
                    // if system not in radiomode then get the portindex from the source IP address
                    if ((recvbytes[0] == remotebytes[0]) && (recvbytes[1] == remotebytes[1]) && (recvbytes[2] == remotebytes[2]) &&
                         (recvbytes[3] >= remotebytes[3]) && (recvbytes[3] < (remotebytes[3] + ncomport)))
                    {
                        portindex = (uint)(recvbytes[3] - remotebytes[3]);
                        data = new byte[buffer.Length];
                        Array.Copy(buffer, 0, data, 0, buffer.Length);
                        MessageReceived(portindex, data);
                    }
                    else
                    {
                        //mw.Log(MhcsLib.DebugZones.Errors, DEVCOM_UDP_RECV_REJECTED, recvendpoint.Address, recvendpoint.Port);
                    }
                }
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                mw.Log(MhcsLib.DebugZones.Errors, DEVCOM_ERR_UDP_RECV, ex.Message);
                if (ex.NativeErrorCode != 10054)
                {
                    //break;
                }
            }
            catch (Exception ex)
            {
                var st = new StackTrace(ex, true);
                mw.Log(MhcsLib.DebugZones.Errors, DEVCOM_ERR_UDP_RECV, ex.Message);
                //break;
            }
        }
        mw.Log(MhcsLib.DebugZones.StartupShutdown, DEVCOM_UDP_RECV_EXITING);
}
Nedo2490
  • 9
  • 1
  • 7
  • Do you know which line throws the exception? – HebeleHododo Dec 29 '16 at 11:15
  • Unfortunately not right now, I should add a log of that... – Nedo2490 Dec 29 '16 at 11:18
  • My best guess is that it involves `recvbytes` or `remotebytes` since those are the only arrays I see whose lengths aren't hard coded and don't have an accompanying length checker before being accessed. – Abion47 Dec 29 '16 at 11:24
  • Changing those magic numbers to constants could help you find the problem while making the code more maintainable. – HebeleHododo Dec 29 '16 at 11:25
  • I will go that way then and see what happens, check on recvbytes and remotebytes @Abion47 and add constants, thanks – Nedo2490 Dec 29 '16 at 11:32

0 Answers0