读取 NetworkStream 不会推进流
我有一个客户端-服务器应用程序,其中服务器传输一个 4 字节整数,指定下一次传输的大小。当我在客户端读取 4 字节整数(指定 FILE_SIZE)时,下次读取流时,我会读取 FILE_SIZE + 4 个字节。
从此流读取时是否需要将偏移量指定为 4,或者是否有办法自动推进 NetworkStream,以便我的偏移量始终为 0?
服务器
NetworkStream theStream = theClient.getStream();
//...
//Calculate file size with FileInfo and put into byte[] size
//...
theStream.Write(size, 0, size.Length);
theStream.Flush();
客户端
NetworkStream theStream = theClient.getStream();
//read size
byte[] size = new byte[4];
int bytesRead = theStream.Read(size, 0, 4);
...
//read content
byte[] content = new byte[4096];
bytesRead = theStream.Read(content, 0, 4096);
Console.WriteLine(bytesRead); // <-- Prints filesize + 4
I have a client-server application where the server transmits a 4-byte integer specifying how large the next transmission is going to be. When I read the 4-byte integer on the client side (specifying FILE_SIZE), the next time I read the stream I get FILE_SIZE + 4 bytes read.
Do I need to specify the offset to 4 when reading from this stream, or is there a way to automatically advance the NetworkStream so my offset can always be 0?
SERVER
NetworkStream theStream = theClient.getStream();
//...
//Calculate file size with FileInfo and put into byte[] size
//...
theStream.Write(size, 0, size.Length);
theStream.Flush();
CLIENT
NetworkStream theStream = theClient.getStream();
//read size
byte[] size = new byte[4];
int bytesRead = theStream.Read(size, 0, 4);
...
//read content
byte[] content = new byte[4096];
bytesRead = theStream.Read(content, 0, 4096);
Console.WriteLine(bytesRead); // <-- Prints filesize + 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的;找到了;
FileInfo.Length
是一个long
;您的调用:写入 8 个字节,little-endian。然后,您可以通过以下方式读回该内容:
which little-endian 将为您提供相同的值(至少对于 32 位)。不过,流中还有 4 个
00
字节未使用(来自long
的高字节) - 因此有 4 个字节不匹配。使用以下之一:
binWrite.Write((int)fileInfo.Length);
filesize = binRead.ReadInt64();
Right; found it;
FileInfo.Length
is along
; your call to:writes 8 bytes, little-endian. You then read that back via:
which little-endian will give you the same value (for 32 bits, at least). You have 4
00
bytes left unused in the stream, though (from the high-bytes of thelong
) - hence the 4 byte mismatch.Use one of:
binWrite.Write((int)fileInfo.Length);
filesize = binRead.ReadInt64();
NetworkStream 确实进步了,但在这两种情况下,您的读取都是不可靠的;经典的“读取已知数量的内容”将是:
with:
另请注意,在解析长度前缀时需要小心字节顺序。
我怀疑您根本没有阅读完整的数据。
NetworkStream
certainly advances, but in both cases, your read is unreliable; a classic "read known amount of content" would be:with:
note also that you need to be careful with endianness when parsing the length-prefix.
I suspect you simply aren't reading the complete data.