I\'m currently working with tcp/ip suite. I\'m writing a program to encrypt files at sender\'s end and decrypt at receiver\'s side. I came across this exception while initia
It is the normal state for a Stream. You need to mentally model it like a small desert river in the spring, trickling water like a Stream is trickling bytes. You don't know when it is going to dry up, that requires a weather forecast that faithfully predicts when it is going to stop raining.
Such weather forecasts certainly exist. No trouble if it is actually a MemoryStream, it has access to all the bytes so it can reliably tell you when it dries up. Or a FileStream, now the operating system's file system can provide the forecast. The directory entry for the file data records the length of the file.
It gets to be a lot harder for TCP streams. The TCP protocol itself doesn't provide the information at all. You can only keep calling the socket's Read() method and when it returns 0 then you know it stopped raining.
This often requires building a protocol on top of TCP. The very common HTTP protocol is a good example. It does provide you with the forecast, it is HttpWebRequest.ContentLength. Or you make your own, the standard technique is to have the transmitter first write 4 bytes that says how much data follows. But NetworkStream doesn't cater to specific protocol implementations, it models a generic network stream. You have to add the weather forecast yourself.