title: c# Source array was not long enough. Check srcIndex and length, and the array's lower bounds for Array.Copy
body: <p>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</p>
  
  <blockquote>
    <p>1) Packet header -- 44 bytes
    This header size doesn't change and comes with every packet.</p>
    
    <p>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).</p>
    
    <p>3) Last 2 bytes are crc which is also fixed and doesn't change.</p>
  </blockquote>
  
  <p>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</p>
  
  <p>server code</p>
  
  <pre><code>private void createserver(int no_of_clients)
      {
          tcpListener = new TcpListener(ipAddress, Globals.port_number);
          tcpListener.Start();
  
          for (int i = 0; i &lt; 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 &lt; 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
      }
  </code></pre>
  
  <p>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 </p>
  
  <blockquote>
  <pre><code>   Array.Copy(pkt, ind, pass_packet, 0, size);
  </code></pre>
  </blockquote>
  
  <p>the 'size' value suddenly jumps to 4096</p>
  
  <p>The stack trace is following</p>
  
  <blockquote>
    <p>at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)</p>
    
    <p>at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)</p>
    
    <p>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</p>
    
    <p>at Datapackparser(Byte[] packet, Int32 input_length, Int32 indexno, Socket sk) in WindowsFormsApplication1\packet_parser.cs:line 847</p>
    
    <p>at WindowsFormsApplication1.Form1.Listeners() in \WindowsFormsApplication1\Form1.cs:line 385</p>
    
    <p>at System.Threading.ThreadHelper.ThreadStart_Context(Object state)</p>
    
    <p>at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)</p>
    
    <p>at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)</p>
    
    <p>at System.Threading.ThreadHelper.ThreadStart()</p>
  </blockquote>
  
  <p>So anyone can help me out with this error?</p>