The nature of streams that are built on top of sockets is that you have an open pipeline that transmits and receives data until the socket is closed.
However, because of the nature of client/server interactions, this pipeline isn't always guaranteed to have content on it to be read. The client and server have to agree to send content over the pipeline.
When you take the Stream
abstraction in .NET and overlay it on the concept of sockets, the requirement for an agreement between the client and server still applies; you can call Stream.Read
all you want, but if the socket that your Stream
is connected to on the other side isn't sending content, the call will just wait until there is content.
This is why protocols exist. At their most basic level, they help define what a complete message that is sent between two parties is. Usually, the mechanism is something along the lines of:
- A length-prefixed message where the number of bytes to be read is sent before the message
- A pattern of characters used to mark the end of a message (this is less common depending on the content that is being sent, the more arbitrary any part of the message can be, the less likely this will be used)
That said you aren't adhering to the above; your call to Stream.Read
is just saying "read 1024 bytes" when in reality, there might not be 1024 bytes to be read. If that's the case, the call to Stream.Read
will block until that's been populated.
The reason the call to Thread.Sleep
probably works is because by the time a second goes by, the Stream
has 1024 bytes on it to read and it doesn't block.
Additionally, if you truly want to read 1024 bytes, you can't assume that the call to Stream.Read
will populate 1024 bytes of data. The return value for the Stream.Read
method tells you how many bytes were actually read. If you need more for your message, then you need to make additional calls to Stream.Read
.
Jon Skeet wrote up the exact way to do this if you want a sample.