0

i wont take much of your time. But im working on a gameserver, sometimes when data has been sent ( packets ) i get this error "Index was outside the bounds of the array".

This is how my code looks like:

   internal void HandleConnectionData(ref byte[] data)
    {
        if (data[0] == 64)
        {
            int pos = 0;

            while (pos < data.Length)
            {
                try
                {
                    int MessageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] });
                    uint MessageId = Base64Encoding.DecodeUInt32(new byte[] { data[pos++], data[pos++] });

                    byte[] Content = new byte[MessageLength - 2];

                    for (int i = 0; i < Content.Length; i++)
                    {
                        Content[i] = data[pos++];
                    }

                    ClientMessage Message = new ClientMessage(MessageId, Content);

                    if (MessageHandler == null)
                    {
                        InitHandler(); //Never ever register the packets BEFORE you receive any data.
                    }

                    //DateTime PacketMsgStart = DateTime.Now;
                    try
                    {
                        MessageHandler.HandleRequest(Message);
                    }
                    catch (Exception e) { Logging.LogPacketException(Message.ToString(), e.ToString()); }

                }
                catch (Exception e)
                {

                    Logging.HandleException(e, "packet handling");
                    Disconnect();

                }
            }
        }
        else
        {
            Connection.SendData(CrossdomainPolicy.GetXmlPolicy());
            Connection.Disposes();
        }
    }

Any ideas? thank you.

  • Is it possible that you receive an empty array in `HandleConnectionData`? So in the `if` condition `data[0]` will throw an exception. Also you seem to increment `pos` in a lot of places. Try to step through and debug your code, that'll help. – Szabolcs Dézsi Feb 28 '16 at 15:08
  • This question is not exactly answered as the problem of the user comes because the TCP implementation, it should be reopened. – Gusman Feb 28 '16 at 15:14

0 Answers0