I open a TcpClient and then call tcpClient.GetStream().Read(message, 0, 8) in order to read messages from the connection.
For some reason I keep getting garbage data even though the other side of the connection does not send any data. Read() never blocks, DataAvailable is always true, and I get a lot of garbage as the data.
What could be the reason?
Thank you in advance for your help!
That is hard to answer without seeing both ends of the pipe (but in particular the sending end).
DataAvailableonly really indicates the state of the local buffer (not the stream itself); in terms of determining the end of a stream it is largely useless (t reports something unrelated).I expect this is a bug in the transmitting code. A classic error here is the following:
When it should be:
The first (and incorrect) version sends the garbage portion of the memory-stream’s backing buffer.