索引和计数必须指向缓冲区内的位置。参数名称:字节

时间:2011-10-05 17:23:01

标签: c# sockets tcp

大家好我即将收到此错误请参阅附件>> Index and count must refer to a location within the buffer. Parameter name: bytes

enter image description here

当我使用调试器时,我没有收到此错误,一切顺利,我无法理解这个错误是什么

这是我的服务器代码:

 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/

2 个答案:

答案 0 :(得分:2)

例外是确切地说明了问题所在:你的一个参数不是你认为应该的。

clientData的长度是多少?致电fileNameLenEncoding.ASCII.GetString的价值是多少? bt的值是多少,用于初始化clientData数组?

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

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

  

如果您使用的是面向连接的套接字,则Receive方法将读取尽可能多的数据,最大可达缓冲区大小。

可能并非所有数据都可用,或者缓冲区小于文件大小。为了确保您获得所有数据,您必须这样做:

int totalBytesRead = 0;
int bytesRead = 0;
while ((bytesRead = clientSock.Receive(clientData, totalBytesRead,
    clientData.Length - totalBytesRead, SocketFlags.None)) != 0)
{
    totalBytesRead += bytesRead;
}
当没有更多可用数据时,

Receive将返回0。只有这样才能确定您已收到所有数据。

答案 1 :(得分:1)

解决方案很简单。 我在Encoding.UTF8.GetString(array[byte], index, array[byte].Length)遇到了类似的问题。 问题是该方法将 TAKE 一些字节数 FROM 数组中的某个位置,因此您必须不是array[byte].Length,而是小于array[byte].Length - index

如果你的阵列有10个元素并且想要从第5个位置获取所有元素,则不能使用10个元素。

所以,再次 - Encoding.UTF8.GetString(array[byte], FROM, TAKE)