title: Index and count must refer to a location within the buffer. Parameter name: bytes
body: <p>Hello guys im getting This error please see the attachment >> <code>Index and count must refer to a location within the buffer. Parameter name: bytes</code></p>
  
  <p><img src="https://i.stack.imgur.com/PMJ7a.jpg" alt="enter image description here"></p>
  
  <p>When im using debugger i dont get this error and everything goes fine i cannot understand what this error is</p>
  
  <p>this is my server code :</p>
  
  <pre><code> IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 27015);
              Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
              sck.Bind(ipEnd);
              sck.Listen(100);
  
              Socket clientSocket = sck.Accept();
  
              string[] fNames = new string[3];
              fNames[0] = "01.jpg";
              fNames[1] = "02.jpg";
              fNames[2] = "03.jpg";
  
              string filePath = "D:\\";
  
              byte[] FilesCount = BitConverter.GetBytes(fNames.Count());
  
  
              clientSocket.Send(FilesCount);
  
              for (int i = 0; i &lt; fNames.Count(); i++)
              {
                  byte[] fileNameByte = Encoding.ASCII.GetBytes(fNames[i]);
                  byte[] fileData = File.ReadAllBytes(filePath + fNames[i]);
                  byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                  byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
  
                  fileNameLen.CopyTo(clientData, 0);
                  fileNameByte.CopyTo(clientData, 4);
                  fileData.CopyTo(clientData, 4 + fileNameByte.Length);
  
                  clientSocket.Send(clientData);
              }
  
              clientSocket.Close();
  </code></pre>
  
  <p>And Client:</p>
  
  <pre><code>   Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
          clientSock.Connect(IPAddress.Parse("46.49.70.30"), 27015);
  
  
          byte[] clientData = new byte[1024 * bt];
          string receivedPath = "D:/df/";
  
          byte[] FileQuantityByte = new byte[1024];
          clientSock.Receive(FileQuantityByte);
          int FileQuantity = BitConverter.ToInt32(FileQuantityByte, 0);
  
          for (int i = 0; i &lt; FileQuantity; i++)
          {
              int receivedBytesLen = clientSock.Receive(clientData);
  
              int fileNameLen = BitConverter.ToInt32(clientData, 0);
              string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
  
              //Console.WriteLine("Client:{0} connected &amp; File {1} started received.", clientSock.RemoteEndPoint, fileName);
  
              BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append));
              bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
  
              //Console.WriteLine("File: {0} received &amp; saved at path: {1}", fileName, receivedPath);
  
              bWrite.Close();
          }
  
          clientSock.Close();
  </code></pre>
  
  <p>EDIT : <a href="http://imageshack.us/f/202/errbk.jpg/" rel="nofollow noreferrer">http://imageshack.us/f/202/errbk.jpg/</a></p>