读取 NetworkStream 不会推进流

发布于 2024-12-11 19:31:29 字数 733 浏览 0 评论 0原文

我有一个客户端-服务器应用程序,其中服务器传输一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-12-18 19:31:29

正确的;找到了; FileInfo.Length 是一个long;您的调用:

binWrite.Write(fileInfo.Length);

写入 8 个字节,little-endian。然后,您可以通过以下方式读回该内容:

filesize = binRead.ReadInt32();

which little-endian 将为您提供相同的值(至少对于 32 位)。不过,流中还有 4 个 00 字节未使用(来自 long 的高字节) - 因此有 4 个字节不匹配。

使用以下之一:

  • binWrite.Write((int)fileInfo.Length);
  • filesize = binRead.ReadInt64();

Right; found it; FileInfo.Length is a long; your call to:

binWrite.Write(fileInfo.Length);

writes 8 bytes, little-endian. You then read that back via:

filesize = binRead.ReadInt32();

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 the long) - hence the 4 byte mismatch.

Use one of:

  • binWrite.Write((int)fileInfo.Length);
  • filesize = binRead.ReadInt64();
故乡的云 2024-12-18 19:31:29

NetworkStream 确实进步了,但在这两种情况下,您的读取都是不可靠的;经典的“读取已知数量的内容”将是:

static void ReadAll(Stream source, byte[] buffer, int bytes) {
  if(bytes > buffer.Length) throw new ArgumentOutOfRangeException("bytes");
  int bytesRead, offset = 0;
  while(bytes > 0 && (bytesRead = source.Reader(buffer, offset, bytes)) > 0) {
    offset += bytesRead;
    bytes -= bytesRead;
  }
  if(bytes != 0) throw new EndOfStreamException();
}

with:

ReadAll(theStream, size, 4);
...
ReadAll(theStream, content, contentLength);

另请注意,在解析长度前缀时需要小心字节顺序。

我怀疑您根本没有阅读完整的数据。

NetworkStream certainly advances, but in both cases, your read is unreliable; a classic "read known amount of content" would be:

static void ReadAll(Stream source, byte[] buffer, int bytes) {
  if(bytes > buffer.Length) throw new ArgumentOutOfRangeException("bytes");
  int bytesRead, offset = 0;
  while(bytes > 0 && (bytesRead = source.Reader(buffer, offset, bytes)) > 0) {
    offset += bytesRead;
    bytes -= bytesRead;
  }
  if(bytes != 0) throw new EndOfStreamException();
}

with:

ReadAll(theStream, size, 4);
...
ReadAll(theStream, content, contentLength);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文