The first exception of type "System.ArgumentOutOfRangeException" exception occurred in mscorlib.dll

Im new to the C # client and server application working on the file client and server application.

I can successfully upload the file name and file data to the server from the client application. but when I try to embed a new text box that allows the file upload client to enter its name and send information along with the file name and file data when he / she clicks the submit 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); 

a window and an error in bold are displayed:

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

thank you in advance.

+4
source share
1 answer

Everything will be simpler if you use BinaryReader / BinaryWriter.

Example:

Client

fileContents is an array of bytes.

 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(); 
+1
source

All Articles