Hello guys im getting This error please see the attachment >> Index and count must refer to a location within the buffer. Parameter name: bytes

When im using debugger i dont get this error and everything goes fine i cannot understand what this error is
this is my server 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 < 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();
And Client:
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 < 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 & 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 & saved at path: {1}", fileName, receivedPath);
bWrite.Close();
}
clientSock.Close();
The exception is telling exactly what the problem is: one of your parameters isn’t what you think it should be.
What is the length of
clientData? What is the value offileNameLenwhen you callEncoding.ASCII.GetString? What is the value ofbt, which is used to initialize yourclientDataarray?If this doesn’t happen in the debugger, then add some code to output the values of
clientDataandfileNameLenbefore the call.One problem is that
clientSock.Receivemight not get all of the data at once. If you’re sending an especially large file, it’s possible thatclientSock.Receivewill return without reading everything. As documentation for Socket.Receive says:It’s possible that not all of the data is available yet, or that the buffer is smaller than the file size. To ensure that you get all of the data, you have to do this:
Receivewill return 0 when there is no more data available. Only then can you be sure that you’ve received all of the data.