try abc

try abc

  • 1.4k
  • 306
  • 486.9k

binaryreader.ReadInt32() reads too big or -ve value

Sep 10 2020 4:37 AM
I am working on client server application where server sends screen image to client using UDT protocol. below is my code server sends image using binary-writer and client receive data using binary-writer. Code works perfectly up to few iteration but after few iteration reader.ReadInt32() read number which is either too big or either negative. i have read most of the binary-reader related issue but not found any helpful.
 
Server Code:
  1. public void SendImage(Bitmap bmp)   
  2. {    
  3.  BinaryWriter writer = new BinaryWriter(CStream);            
  4.   using (MemoryStream ms=new MemoryStream())  
  5.   {     
  6.      bmp.Save(ms, ImageFormat.Jpeg);//save to memory stream        
  7.      long fileSize = ms.Length;         
  8.      writer.Write(fileSize); //send length                    
  9.      byte[] buffer = new byte[512 * 1024];    
  10.      long pos = 0;     
  11.      ms.Seek(0, SeekOrigin.Begin);     
  12.      while (pos < fileSize)    
  13.      {   
  14.         //If buffer size is smaller then filesize then send in parts     
  15.         Int32 toSend = buffer.Length < (fileSize - pos)? buffer.Length: (Int32)(fileSize - pos);   
  16.         ms.Read(buffer, 0, toSend); //read from memory stream to byte array            
  17.         writer.Write(toSend);   
  18.         ServerSocket.Send(buffer, 0, toSend);  
  19.         pos += toSend;  
  20.         Console.WriteLine(itration + ", fileSize : "+ fileSize + "," + toSend + ",[buffer]: " + buffer[toSend - 3] + "," + buffer[toSend - 2] + ", " + buffer[toSend-1]);  
  21.      }          
  22.    itration++;                    
  23.    ms.Flush();          
  24.    ms.Close();    
  25.    }  
  26. }  
Client Side Code:
  1. private void startRead()   
  2. {    
  3.     while (true)    
  4.     {  
  5.         Bitmap inImage = ReceiveImage(Settings.Encrypted);//BitmapFromStream(Settings.Encrypted);  
  6.         theImage.Image = (Image)inImage;    
  7.     }  
  8. }   
  9. private Bitmap ReceiveImage(bool Encrypted)   
  10. {               
  11.    Bitmap img = null;               
  12.    byte[] buffer;              
  13.    BinaryReader reader = new BinaryReader(CStream);        
  14.    using(MemoryStream ms=new MemoryStream())  
  15.    {   
  16.       try   
  17.       {  
  18.          long size = reader.ReadInt64();//read file length first  
  19.          buffer = new byte[4 * 1024 * 1024];  
  20.          long read = 0;  
  21.          while (read < size)  
  22.          {  
  23.             int toRecv = reader.ReadInt32(); //here reader reads wrong value  
  24.             Console.WriteLine(itration + "filesize " + size + " Byte to read : " + toRecv);  
  25.             ReadFragment(reader, toRecv, buffer);   
  26.             ms.Write(buffer, 0, toRecv);  
  27.             read += toRecv;   
  28.          }       
  29.          itration++;  
  30.          ms.Position = 0;  
  31.          img = new Bitmap(ms);  
  32.        }   
  33.        catch (Exception Ex)   
  34.        {   
  35.                     Console.WriteLine(Ex.Message + " : " + Ex.StackTrace);       
  36.        }   
  37.        buffer = null;    
  38.        ms.Flush();    
  39.        ms.Close();   
  40.      }   
  41.     reader = null;    
  42.    return img;   
  43. }   
  44. private int ReadFragment(BinaryReader reader, int size, byte[] buffer)   
  45. {   
  46.   int read = 0;   
  47.   while (read < size)         
  48.   {           
  49.     read += reader.Read(buffer, read, size - read);  
  50.   }    
  51.   return read;  
  52. }   
this is the console output from server
 
0, fileSize : 568633,524288,[buffer]: 0,162, 138
0, fileSize : 568633,44345,[buffer]: 31,255, 217
1, fileSize : 568468,524288,[buffer]: 2,138, 40
1, fileSize : 568468,44180,[buffer]: 3,255, 217
2, fileSize : 578806,524288,[buffer]: 198,177, 106
2, fileSize : 578806,54518,[buffer]: 1,255, 217
3, fileSize : 589967,524288,[buffer]: 20,81, 64
3, fileSize : 589967,65679,[buffer]: 15,255, 217
11, fileSize : 632653,524288,[buffer]: 243,255, 0
11, fileSize : 632653,108365,[buffer]: 0,255, 217
12, fileSize : 642562,524288,[buffer]: 205,20, 85
12, fileSize : 642562,118274,[buffer]: 7,255, 217
13, fileSize : 645552,524288,[buffer]: 236,49, 131
13, fileSize : 645552,121264,[buffer]: 0,255, 217
 
and console output from client
 
0 filesize 568633 Byte to read : 524288
0 filesize 568633 Byte to read : 44345
1 filesize 568468 Byte to read : 524288
1 filesize 568468 Byte to read : 44180
2 filesize 578806 Byte to read : 524288
2 filesize 578806 Byte to read : 54518
3 filesize 589967 Byte to read : 524288
3 filesize 589967 Byte to read : 65679
11 filesize 632653 Byte to read : 524288
11 filesize 632653 Byte to read : 108365
12 filesize 642562 Byte to read : 524288
12 filesize 642562 Byte to read : 118274
13 filesize 645552 Byte to read : 524288
13 filesize 645552 Byte to read : 671776930 <-- here it read either too big number or negative number Exception thrown: 'System.ArgumentException' in mscorlib.dll
 
I have tried binaryformater, protobuf instead of binaryreader but it still stops after few itration.help me find the cause.

Answers (1)