c# 源数组不够长。检查 srcIndex 和长度,以及 Array.Copy 的数组下限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18457914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:15:23  来源:igfitidea点击:

c# Source array was not long enough. Check srcIndex and length, and the array's lower bounds for Array.Copy

c#exception

提问by prattom

I have implemented multiple client and server. The client sends tcp packet of size 238 or 564 to server at fixed interval of 2 minutes. The structure of packets is following

我已经实现了多个客户端和服务器。客户端以固定的 2 分钟间隔向服务器发送大小为 238 或 564 的 tcp 数据包。数据包结构如下

1) Packet header -- 44 bytes This header size doesn't change and comes with every packet.

2) After header comes more than 1 data packets and these data packets are of size 16 or 32 bytes. The number of these data packets changes with every packet coming from client to server and number of these data packets decide total size of packet (238 or 512).

3) Last 2 bytes are crc which is also fixed and doesn't change.

1) 包头——44 字节 这个头大小不会改变,并且随每个包一起提供。

2) 报头后有 1 个以上的数据包,这些数据包的大小为 16 或 32 字节。这些数据包的数量随着从客户端到服务器的每个数据包而变化,这些数据包的数量决定了数据包的总大小(238 或 512)。

3) 最后 2 个字节是 crc,也是固定不变的。

My server receives packet from client and separate data packets from main packet, parse them and write it to excel file. Following is my code

我的服务器从客户端接收数据包并将数据包与主数据包分开,解析它们并将其写入excel文件。以下是我的代码

server code

服务器代码

private void createserver(int no_of_clients)
    {
        tcpListener = new TcpListener(ipAddress, Globals.port_number);
        tcpListener.Start();

        for (int i = 0; i < no_of_clients; i++)
        {
            Thread newThread = new Thread(new ThreadStart(Listeners));
            newThread.Start();
        }
    } //End of createserver();

public void Listeners()
    {
        Socket socketForClient;

            socketForClient = tcpListener.AcceptSocket();



        if (socketForClient.Connected)
        {
            NetworkStream networkStream = new NetworkStream(socketForClient);

            int stream_size = 0;
            while (true)
            {
                byte[] raw_stream = new byte[1024];

                do
                {
                    try
                    {
                        stream_size = networkStream.Read(raw_stream, 0, 1024);
                    }
                    catch (IOException e)
                    {
                        if (e.InnerException is SocketException)
                        {
                            MessageBox.Show("The client has disconnected");
                            foreach (Socket s in active_clients)
                            {
                                if (s == socketForClient)
                                {
                                    MessageBox.Show(string.Format("Client {0} has forcibly exited", s.RemoteEndPoint));
                                    infoBox1.Text = infoBox1.Text + "\r\n" + string.Format("Client {0} has forcibly exited", s.RemoteEndPoint);
                                }
                            }
                        }
                        return;
                    }
                }
                while (networkStream.DataAvailable);

                byte[] input_data = new byte[stream_size];
                byte[] input_data1 = new byte[stream_size];
                Array.Copy(raw_stream, 0, input_data, 0, stream_size);

                if (Encoding.ASCII.GetString(input_data) != Globals.exit_code)
                {
                  Datapackparser(input_data, input_data.Length, 0, socketForClient);
                }
         }
      }

public static void Datapackparser(byte[] packet, int input_length, int indexno, Socket sk))
    {
        //remove header and crc from end of packet since I know every time it will be same                     

          // for separating data packets and parsing them on basis of packet id which comes with every individual data packets 

            data_pkts_index = 44; // since data packets start at 44. 0-43 is header
            int size_1_data_pkts = 0;
            string pkt_ids = "";

            while (data_pkts_index < tcp_pkt_size - 2)
            {
                // first 2 bytes of each data packet is size of that data packet 16 or 32
                size_1_data_pkts = Convert.ToInt32(string.Format(BitConverter.ToString(packet, data_pkts_index + 1, 1) +
                    BitConverter.ToString(packet, data_pkts_index, 1)), 16);
                // next 1 byte is packet id of each data packet on basis of which I parse them
                pkt_ids = Convert.ToInt32(packet[data_pkts_index + 2]).ToString("X");
                // this function is for parsing each data packet
                data_pkt_func(data_pkts_index, size_1_data_pkts, pkt_ids, packet, imei);
                data_pkts_index = data_pkts_index + size_1_data_pkts;time it will be same.

           }
       }

 static private void data_pkt_func(int ind, int size, string code_packet, byte[] pkt, string file1)
    {
        byte[] pass_packet = new byte[size];
        Array.Copy(pkt, ind, pass_packet, 0, size);

        if (code = "a")
        { // one type of packet
        }
        else if (code = "dsd")
        { // 2nd type of packet
        }
        else if ......
        {
         }
       // like this their are 8-9 types of data packet
    }

Problem I am facing is that this code is able to parse the packets correctly to some extent. For example the first 10-11 packets the server receives are parsed correctly but then after that exception "Source array was not long enough. Check srcIndex and length, and the array's lower bounds" is thrown at line

我面临的问题是这段代码能够在某种程度上正确解析数据包。例如,服务器接收到的前 10-11 个数据包被正确解析,但在该异常之后“源数组不够长。检查 srcIndex 和长度,以及数组的下限”被抛出

   Array.Copy(pkt, ind, pass_packet, 0, size);
   Array.Copy(pkt, ind, pass_packet, 0, size);

the 'size' value suddenly jumps to 4096

'size' 值突然跳到 4096

The stack trace is following

堆栈跟踪如下

at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)

at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)

at Packet_parsing.client_pkt_parsing.data_pkt_func(Int32 ind, Int32 size, String code_packet, Byte[] pkt, String file1) in WindowsFormsApplication1\packet_parser.cs:line 357

at Datapackparser(Byte[] packet, Int32 input_length, Int32 indexno, Socket sk) in WindowsFormsApplication1\packet_parser.cs:line 847

at WindowsFormsApplication1.Form1.Listeners() in \WindowsFormsApplication1\Form1.cs:line 385

at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()

在 System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean 可靠)

在 System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)

在 Packet_parsing.client_pkt_parsing.data_pkt_func(Int32 ind, Int32 size, String code_packet, Byte[] pkt, String file1) in WindowsFormsApplication1\packet_parser.cs:line 357

在 WindowsFormsApplication1\packet_parser.cs:line 847 中的 Datapackparser(Byte[] packet, Int32 input_length, Int32 indexno, Socket sk)

在 WindowsFormsApplication1.Form1.Listeners() 在 \WindowsFormsApplication1\Form1.cs:line 385

在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)

在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

在 System.Threading.ThreadHelper.ThreadStart()

So anyone can help me out with this error?

所以任何人都可以帮助我解决这个错误?

采纳答案by piojo

Have you looked at the packet in a hex editor to see whether the packet size is written correctly in the packet? I would start by catching the exception and dumping the packet to a file for further examination.

你有没有在十六进制编辑器中查看过数据包,看看数据包中的数据包大小是否正确写入?我将首先捕获异常并将数据包转储到文件中以供进一步检查。

Also, are you sure your number parsing code is right? The first "Convert.ToInt32" line is complex, and I can't find any documentation that lists "Convert.ToInt32" as taking two parameters. I suspect if you fix that line, you will fix the issue.

另外,您确定您的号码解析代码正确吗?第一个“Convert.ToInt32”行很复杂,我找不到任何将“Convert.ToInt32”列为采用两个参数的文档。我怀疑如果你修复了那条线,你就会解决这个问题。

回答by prattom

Ok I found out that the problem was on client side. Client was adding extra 0 to data packets after sending 10-12 packets correctly due to some bug on client side. I had no access to client side and I had coded my server on basis of specification given to me thus I didn't know about the bug and client side developer also didn't knew about this bug. Thanks to all answers.

好的,我发现问题出在客户端。由于客户端的一些错误,客户端在正确发送 10-12 个数据包后向数据包添加了额外的 0。我无法访问客户端,并且我根据给定的规范对服务器进行了编码,因此我不知道这个错误,客户端开发人员也不知道这个错误。感谢所有答案。