BinaryReader 的奇怪行为
我有一个基于套接字的应用程序,它在客户端使用 BinaryReader
对象公开接收到的数据。我一直在尝试调试读取器中包含的数据不干净的问题...即我正在读取的缓冲区包含的旧数据超过了新数据的大小。
在下面的代码中:
System.Diagnostics.Debug.WriteLine("Stream length: {0}", _binaryReader.BaseStream.Length);
byte[] buffer = _binaryReader.ReadBytes((int)_binaryReader.BaseStream.Length);
当我注释掉第一行时,数据最终不会像我有该打印行语句那样变脏(或者不会像常规那样最终变脏)。据我所知,从服务器端数据是干净地传入的,因此我的套接字实现可能存在一些问题。但是有人知道为什么添加打印行会导致数据更频繁地变脏吗?
I have a socket-based application that exposes received data with a BinaryReader
object on the client side. I've been trying to debug an issue where the data contained in the reader is not clean... i.e. the buffer that I'm reading contains old data past the size of the new data.
In the code below:
System.Diagnostics.Debug.WriteLine("Stream length: {0}", _binaryReader.BaseStream.Length);
byte[] buffer = _binaryReader.ReadBytes((int)_binaryReader.BaseStream.Length);
When I comment out the first line, the data doesn't end up being dirty (or, doesn't end up being dirty as regularly) as when I have that print line statement. As far as I can tell, from the server side the data is coming in cleanly, so it's possible that my socket implementation has some issues. But does anyone have any idea why adding that print line would cause the data to be dirty more often?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的二进制阅读器看起来像是一个私有成员变量(如果前导下划线是一个告诉标志)。
您的应用程序是多线程的吗?如果在您读取时另一个线程也尝试使用您的 binaryReader,您可能会遇到竞争状况。即使没有那条线,您也会遇到问题,这一事实对我来说似乎很可疑。
Your binary reader looks like it is a private member variable (if the leading underscore is a tell tell sign).
Is your application multithreaded? You could be experiencing a race condition if another thread is attempting to do also use your binaryReader while you are reading from it. The fact that you experience issues even without that line seems quite suspect to me.
你确定你的阅读逻辑是正确的吗?
Stream.Length
表示整个流的长度,而不是要读取的剩余数据的长度。假设最初有 100 个字节可用。
Length
为 100,BinaryReader
纠正读取的 100 个字节并将流位置前进 100。然后,另外 20 个字节到达。长度
现在是120;但是,您的BinaryReader
应该只读取 20 个字节,而不是 120 个字节。第二次读取中请求的“额外”100 个字节会导致其阻塞或(如果流未正确实现)中断。Are you sure that your reading logic is correct?
Stream.Length
indicates the length of the entire stream, not of the remaining data to be read.Suppose that, initially, 100 bytes were available.
Length
is 100, andBinaryReader
corrects reads 100 bytes and advances the stream position by 100. Then, another 20 bytes arrive.Length
is now 120; however, yourBinaryReader
should only be reading 20 bytes, not 120. The ‘extra’ 100 bytes requested in the second read would either cause it to block or (if the stream is not implemented correctly) break.这个问题很愚蠢而且无关紧要。不过,我相信我上面的阅读逻辑是正确的。问题是我使用的 _binaryReader 是一个不属于我的类所拥有的引用,因此底层流正在被错误的数据重写。
The problem was silly and unrelated. I believe my reading logic above is correct, however. The issue was that the _binaryReader I was using was a reference that was not owned by my class and hence the underlying stream was being rewritten with bad data.