mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的第一次机会异常

时间:2012-08-09 10:02:39

标签: c# byte bytebuffer

我是c#客户端和服务器应用程序的新手,我正在处理文件上传客户端和服务器应用程序。

我可以从客户端应用程序成功将我的文件名和文件数据上传到服务器。但是当我尝试实现一个新的文本框,允许文件上传客户端输入他/她的名字,并在他/她点击发送按钮时将信息与文件名和文件数据一起发送。

客户端应用程序。

 /* file name and file length */
            byte[] fName = Encoding.UTF8.GetBytes(fileName);
            byte[] fNameLen = BitConverter.GetBytes(fileName.Length); // length of file name
            clientData = new byte[4 + fileName.Length];                
            System.Diagnostics.Debug.WriteLine("fName " + fName.Length);
            System.Diagnostics.Debug.WriteLine("fNamelen " + fNameLen.Length);
            fNameLen.CopyTo(clientData, 0);
            fName.CopyTo(clientData, 4);

            /* author name and author name length */
            byte[] aName = Encoding.UTF8.GetBytes(textBox2.Text);
            byte[] aNameLen = BitConverter.GetBytes(textBox2.Text.Length);
            System.Diagnostics.Debug.WriteLine("aName " + aName.Length);
            System.Diagnostics.Debug.WriteLine("aNamelen " + aNameLen.Length);
            authorData = new byte[9 + textBox2.Text.Length];
            aNameLen.CopyTo(authorData, 5);
            aName.CopyTo(authorData, 9);

服务器应用程序

/* retriving of file name */
               System.Diagnostics.Debug.WriteLine("Error 1");
               fNameLen = BitConverter.ToInt32(state.buffer, 0);
               System.Diagnostics.Debug.WriteLine("Error 2 fNameLen " + fNameLen);
               string Filename = Encoding.UTF8.GetString(state.buffer, 4, fNameLen);
               System.Diagnostics.Debug.WriteLine("Error 3");
               System.Diagnostics.Debug.WriteLine("filename length " + fNameLen);
               receivedPath = @"C:\testfiles\" + Filename;
               System.Diagnostics.Debug.WriteLine("bytesREad1 " + bytesRead);
               System.Diagnostics.Debug.WriteLine(receivedPath);

               /* retriving of author name */
               aNameLen = BitConverter.ToInt32(state.buffer, 5);
               System.Diagnostics.Debug.WriteLine("Error 4");
               System.Diagnostics.Debug.WriteLine("error  5");
               System.Diagnostics.Debug.WriteLine("author name length " + aNameLen);
               string authorName = ASCIIEncoding.ASCII.GetString(state.buffer, 9, aNameLen);
               System.Diagnostics.Debug.WriteLine("Error 6");
               System.Diagnostics.Debug.WriteLine("author name " + authorName);
               System.Diagnostics.Debug.WriteLine("author name length " + aNameLen);

输出窗口和以粗体显示的错误:

  • 错误1
  • 错误2 fNameLen 12
  • 错误3
  • 文件名长度12
  • bytesREad1 82
  • C:\ testfiles \ Test1122.txt
  • 错误4
  • 错误5
  • 作者姓名长度829715301
  • 类型'System.ArgumentOutOfRangeException'的第一次机会异常 发生在mscorlib.dll

提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您使用BinaryReader / BinaryWriter,情况会更容易。

示例:

<强>客户端

fileContents是一个字节数组。

var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
writer.Write(fileName);
writer.Write(authorName);
writer.Write(fileContents.Length);
writer.Write(fileContents);

var data = stream.ToArray();  // send this data array to server
writer.Dispose();
stream.Dispose();

服务器

var stream = new MemoryStream(state.buffer);
var reader = new BinaryReader(stream);

var fileName = reader.ReadString();
var author = reader.ReadString();
var fileContents = reader.ReadBytes(reader.ReadInt32());
reader.Dispose();
stream.Dispose();