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

发布于 2024-12-08 11:52:02 字数 2831 浏览 0 评论 0原文

大家好,我收到此错误,请参阅附件>>> 索引和计数必须引用缓冲区内的位置。参数名称:字节

在此处输入图像描述

当我使用调试器时,我不会收到此错误,一切正常我无法理解这个错误是什么,

这是我的服务器代码:

 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/

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

enter image description here

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

EDIT : http://imageshack.us/f/202/errbk.jpg/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2024-12-15 11:52:02

例外情况是准确说明问题所在:您的参数之一不是您认为应该的。

clientData 的长度是多少?当您调用Encoding.ASCII.GetString时,fileNameLen的值是多少?用于初始化 clientData 数组的 bt 的值是多少?

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

一个问题是 clientSock.Receive 可能无法一次获取所有数据。如果您要发送一个特别大的文件,clientSock.Receive 可能会返回而不读取所有内容。正如 Socket.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。只有这样您才能确定您已收到所有数据。

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 of fileNameLen when you call Encoding.ASCII.GetString? What is the value of bt, which is used to initialize your clientData array?

If this doesn't happen in the debugger, then add some code to output the values of clientData and fileNameLen before the call.

One problem is that clientSock.Receive might not get all of the data at once. If you're sending an especially large file, it's possible that clientSock.Receive will return without reading everything. As documentation for Socket.Receive says:

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer.

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:

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

Receive will return 0 when there is no more data available. Only then can you be sure that you've received all of the data.

海的爱人是光 2024-12-15 11:52:02

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

如果您的数组有 10 个元素,并且您想从第 5 个位置获取所有元素,则不能获取 10 个元素。

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

Solution is simple.
I've got similar problem on Encoding.UTF8.GetString(array[byte], index, array[byte].Length).
Problem is that method will TAKE some count of bytes FROM some position in your array, so you must take not array[byte].Length, but less than array[byte].Length - index.

If your array has 10 elements and you want to take all from 5th position, you can't take 10 elements.

So, againEncoding.UTF8.GetString(array[byte], FROM, TAKE)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文