I am using a networking protocol built around TcpClient
, using BinaryReader
to read bytes from the underlying NetworkStream
(and, conversely, using BinaryWriter
to write).
The protocol transmits strings in UTF-8 encoding, and is calling reader.ReadString()
to read them from the stream (using writer.Write(someStr)
to write).
Is there an easy way to determine the number of bytes read from (or written to) the NetworkStream, without having to jump through hoops to calculate the actual byte lengths of the strings transmitted?
Note that BinaryWriter.Write()
writes a 7-bit-encoded integer before the actual bytes of the string, which makes any manual calculation additionally complex.
Also note that NetworkStream
does not support the Position
property, since it complains about not being able to Seek
.
Furthermore, I would like to avoid introducing intermediaries that have to copy/scan data into the process of reading/writing as not to affect the performance of the whole system.
Is there an easy, high-level way of counting bytes passing through the network interface without having to manually account for the encoding and lengths of the strings?