tags:

views:

40

answers:

2

I've been working on something that make use of streams and I found myself not clear about some stream concepts( you can also view another question posted by me at http://stackoverflow.com/questions/2933923/about-redirected-stdout-in-system-diagnostics-process ).

1.how do you indicate that you have finished writing a stream, writing something like a EOF?

2.follow the previous question, if I have written a EOF(or something like that) to a stream but didn't close the stream, then I want to write something else to the same stream, can I just start writing to it and no more set up required?

3.if a procedure tries to read a stream(like the stdin ) that no one has written anything to it, the reading procedure will be blocked,finally some data arrives and the procedure will just read till the writing is done,which is indicated by getting a return of 0 count of bytes read rather than being blocked, and now if the procedure issues another read to the same stream, it will still get a 0 count and return immediately while I was expecting it will be blocked since no one is writing to the stream now. So does the stream holds different states when the stream is opened but no one has written to it yet and when someone has finished a writing session?

I'm using Windows the .net framework if there will by any thing platform specific.

Thanks a lot!

+3  A: 

This depends on the concrete stream. For example, reading from a MemoryStream would not block as you describle. This is because a MemoryStream has an explicit size, and as you read from the stream the pointer is progressed through the stream untile you reach the end, at which point the Read will return 0. If there was not data in the MemoryStream the first Read would have immediately returned 0.

What you describe fits with a NetworkStream, in which case reading from the stream will block until data becomes available, when the "server" side closes the underlying Socket that is wrapped by the NetworkStream the Read will return 0.

So the actual details depends on the stream, but at the high level they are all treated the same ie. You can read from a stream until the Read returns 0.

Chris Taylor
Does the stdin stdout streams work much the same way as NetWorkStream?
sforester
@sforester, stdin/stdout are non-random access streams like NetworkStream so there is no predetermination of when the stream data ends, the stream reads will end when the stream is closed.
Chris Taylor
+1  A: 

There is no "EOF" with streams. You write to a stream until you close it, which prevents it from being written further.

Streams just read and write bytes. That's all.

John Saunders