类型'System.ArgumentOutOfRangeException'第一次机会异常出现在mscorlib.dll [英] A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

查看:2878
本文介绍了类型'System.ArgumentOutOfRangeException'第一次机会异常出现在mscorlib.dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



客户端应用程序。

  / *文件名和文件长度* / 
byte [] fName = Encoding.UTF8.GetBytes(fileName);
byte [] fNameLen = BitConverter.GetBytes(fileName.Length); //文件名长度
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);

/ *作者名和作者名长度* /
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);

服务器应用程序

  / *检索文件名* / 
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);

/ *检索作者姓名* /
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

  • .ArgumentOutOfRangeException'
    发生在mscorlib.dll中



提前感谢。

$ b



例如:

>

客户



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(); //将此数据数组发送到服务器
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();


Im new to c# client and server application, im working on a file upload client and server application.

i can successfully upload my file name and file data to the server from the client application. but when i try to implement a new textbox that allow the file upload client to enter his/her name and send the information together with the file name and file data when he/her click the send button.

client application.

 /* 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);

server application

/* 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);

output window and error given in bold:

  • Error 1
  • Error 2 fNameLen 12
  • Error 3
  • filename length 12
  • bytesREad1 82
  • C:\testfiles\Test1122.txt
  • Error 4
  • error 5
  • author name length 829715301
  • A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

thank you in advance.

解决方案

Things will be easier if you use BinaryReader / BinaryWriter.

Example:

Client

fileContents is a byte array.

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

Server

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

这篇关于类型'System.ArgumentOutOfRangeException'第一次机会异常出现在mscorlib.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆