我有这个作为客户端的 c# 程序从服务器接收文件。有时它可以无缝运行。有时它会在fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
.
ArgumentOutOfRange Exception
Index and count must refer to a location within the buffer.
Parameter name: bytes
如果值为fileNameLen
is 8
or 12
then 它可以正常工作。否则会1330795077
。这是为什么?谁能解释我为什么会这样?请。这是我的代码。
string fileName = string.Empty;
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
lock (this)
{
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\";
ns.Read(dataByte, thisRead, blockSize);
int fileNameLen = BitConverter.ToInt32(dataByte, 0);
fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
Stream fileStream = File.OpenWrite(folderPath + fileName);
fileStream.Write(dataByte, 4 + fileNameLen, (1024 - (4 + fileNameLen)));
while (true)
{
thisRead = ns.Read(dataByte, 0, blockSize);
fileStream.Write(dataByte, 0, thisRead);
if (thisRead == 0)
break;
}
fileStream.Close();
}