I have a server that sends telemetry data of varying size to receivers, or as I'll call them, clients, via NetworkStream
. I aim for maximum speed, so I want minimal delays. The frequency is uncontrolled, so I'd like to use an infinite loop in my clients, that uses NetworkStream.Read
to read the data, process it, then repeat.
My problem is that sometimes, if two packets are sent very quickly, and a client has a slow internet connection, the two packets will be received as a continous stream, resulting unprocessable data. A half-solution I found (mostly to confirm that this is indeed the error) is to have a small delay after/before each transmission, using System.Threading.Thread.Sleep(100)
, but not only I find Sleep
a botchy solution, it's inconsistent, as it also slows down clients with a good connection, and the problem may persist with an even worse connection.
What I'd like to do is to have the server send a gap between each transmission, providing a separation regardless of internet speed, as NetworkStream.Read
should finish after the current continous stream ends. I don't understand deeply the working of a NetworkStream
, and have no idea what a few bytes of empty stream look like or how it could be implemented. Any ideas?