索引和计数必须引用缓冲区内的位置。参数名称:字节
大家好,我收到此错误,请参阅附件>>> 索引和计数必须引用缓冲区内的位置。参数名称:字节
当我使用调试器时,我不会收到此错误,一切正常我无法理解这个错误是什么,
这是我的服务器代码:
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例外情况是准确说明问题所在:您的参数之一不是您认为应该的。
clientData
的长度是多少?当您调用Encoding.ASCII.GetString
时,fileNameLen
的值是多少?用于初始化clientData
数组的bt
的值是多少?如果调试器中没有发生这种情况,请在调用之前添加一些代码以输出
clientData
和fileNameLen
的值。一个问题是
clientSock.Receive
可能无法一次获取所有数据。如果您要发送一个特别大的文件,clientSock.Receive
可能会返回而不读取所有内容。正如 Socket.Receive 的文档所述:可能并非所有数据都可用,或者缓冲区小于文件大小。为了确保您获得所有数据,您必须执行以下操作:
当没有更多可用数据时,
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 offileNameLen
when you callEncoding.ASCII.GetString
? What is the value ofbt
, which is used to initialize yourclientData
array?If this doesn't happen in the debugger, then add some code to output the values of
clientData
andfileNameLen
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 thatclientSock.Receive
will 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:
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.解决办法很简单。
我在
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 thanarray[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, again —
Encoding.UTF8.GetString(array[byte], FROM, TAKE)