Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading NetworkStream doesn't advance stream

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 
like image 262
jkh Avatar asked Jun 03 '26 16:06

jkh


1 Answers

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();
like image 109
Marc Gravell Avatar answered Jun 05 '26 07:06

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!