When I am reading a message from a network stream I use a function that contains a loop like this
While Mynetworkstream.DataAvailable
'Read the data from the stream
End While
The sender only writes a bufferlength of maybe 4k into the stream at a time that is then read by the receiver in this loop. Now, what happens when the sender isn't very fast and writes the blocks into the stream slower than then receiver reads them, shouldn't .DataAvailable result to false at some point, even though the sender isn't finished sending all it's stuff? If so, how should this be avoided? I only found code like this and it seems to work, but I never understood why this is reliable.
I am using a TCPClient/Server in VB.NET, but answers in C# are also nice, it's all the same concept anyways.