大家好,我收到此错误,请参阅附件>> Index and count must refer to a location within the buffer. Parameter name: bytes
当我使用调试器时,我没有收到此错误,一切正常,我无法理解此错误是什么

这是我的服务器代码:

 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();

和客户:
   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();

编辑:http://imageshack.us/f/202/errbk.jpg/

最佳答案

异常(exception)是确切说明了问题所在:您的参数之一不是您认为应该的。
clientData的长度是多少?调用fileNameLenEncoding.ASCII.GetString的值是什么?用来初始化bt数组的clientData的值是什么?

如果在调试器中没有发生这种情况,请在调用之前添加一些代码以输出clientDatafileNameLen的值。

一个问题是clientSock.Receive可能无法一次获取所有数据。如果您要发送一个特别大的文件,则clientSock.Receive可能会在不读取所有内容的情况下返回。正如Socket.Receive的文档所述:



可能并非所有数据都可用,或者缓冲区小于文件大小。为了确保获得所有数据,您必须执行以下操作:

int totalBytesRead = 0;
int bytesRead = 0;
while ((bytesRead = clientSock.Receive(clientData, totalBytesRead,
    clientData.Length - totalBytesRead, SocketFlags.None)) != 0)
{
    totalBytesRead += bytesRead;
}

没有更多数据可用时,Receive将返回0。只有这样,您才能确定已收到所有数据。

关于c# - 索引和计数必须引用缓冲区中的位置。参数名称: bytes,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7665162/

10-09 06:57