C# Binaryreader Readint32()返回太大的值或-ve值
我正在开发客户机-服务器应用程序,其中服务器使用UDT ptotocol向客户机发送屏幕图像。下面是我的代码服务器使用二进制编写器发送图像,客户端使用二进制编写器接收数据。代码在几次迭代后都能正常工作,但在几次迭代后,reader.ReadInt32()读取的数字可能太大,也可能是负数。我已经阅读了大多数与二进制读取器相关的问题,但没有发现任何有用的东西 服务器代码:C# Binaryreader Readint32()返回太大的值或-ve值,c#,networkstream,binaryreader,C#,Networkstream,Binaryreader,我正在开发客户机-服务器应用程序,其中服务器使用UDT ptotocol向客户机发送屏幕图像。下面是我的代码服务器使用二进制编写器发送图像,客户端使用二进制编写器接收数据。代码在几次迭代后都能正常工作,但在几次迭代后,reader.ReadInt32()读取的数字可能太大,也可能是负数。我已经阅读了大多数与二进制读取器相关的问题,但没有发现任何有用的东西 服务器代码: public void SendImage(Bitmap bmp) { BinaryWriter writer = new
public void SendImage(Bitmap bmp)
{
BinaryWriter writer = new BinaryWriter(CStream);
using (MemoryStream ms=new MemoryStream())
{
bmp.Save(ms, ImageFormat.Jpeg);//save to memory stream
long fileSize = ms.Length;
writer.Write(fileSize); //send length
byte[] buffer = new byte[512 * 1024];
long pos = 0;
ms.Seek(0, SeekOrigin.Begin);
while (pos < fileSize)
{
//If buffer size is smaller then filesize then send in parts
Int32 toSend = buffer.Length < (fileSize - pos)? buffer.Length: (Int32)(fileSize - pos);
ms.Read(buffer, 0, toSend); //read from memory stream to byte array
writer.Write(toSend);
ServerSocket.Send(buffer, 0, toSend);
pos += toSend;
Console.WriteLine(itration + ", fileSize : "+ fileSize + "," + toSend + ",[buffer]: " + buffer[toSend - 3] + "," + buffer[toSend - 2] + ", " + buffer[toSend-1]);
}
itration++;
ms.Flush();
ms.Close();
}
}
和来自客户端的colsole输出
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
0要读取的文件大小568633字节:524288
0要读取的文件大小为568633字节:44345
1要读取的文件大小为568468字节:524288
1要读取的文件大小为568468字节:44180
2要读取的文件大小为578806字节:524288
2要读取的文件大小为578806字节:54518
3要读取的文件大小为589967字节:524288
3要读取的文件大小为589967字节:65679
11要读取的文件大小为632653字节:524288
11要读取的文件大小为632653字节:108365
12要读取的文件大小为642562字节:524288
12要读取的文件大小为642562字节:118274
13要读取的文件大小为645552字节:524288
13要读取的文件大小645552字节:671776930文件损坏或向文件中写入了额外值,因此数据被移位。检查写入数据的代码,以确保写入了不同的值。可以,但如何检查数据是否被刷新或移位?我建议将序列化/反序列化数据的代码与通过网络发送/接收数据的代码分开。这将允许您彼此独立地进行测试,并使类似这样的问题更容易诊断。您需要检查二进制格式的规范,以查看是否应该有额外的数据。有3种可能性1)文件损坏2)应该有额外的数据3)写入数据的程序写入了错误的数据。二进制数据应该有一个CRC,这样当它被传输时,你就可以知道它什么时候被破坏了。在工作中,我看到很多数据的CRC都不好。有些是可以解释的,因为传输模式经常改变,所以我们在数据开始时得到一些CRC错误。但在传输过程中,我也看到了一些错误。
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
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