1

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.

Jens
  • 6,275
  • 2
  • 25
  • 51
  • `NetworkStream.DataAvailable` returns `Socket.Available != 0`, which is `false` when there is no buffered data. See duplicate. :-) You can test this by connecting to an HTTP server, which doesn't send anything until you do. And until it sends you any response which ends up in your socket, `Available` is `0` and `DataAvailable` `false`. – CodeCaster Jun 04 '14 at 20:31

0 Answers0