Stream Reading -- Again

T

Trecius

I've made a post last Friday in regards to this subject, but I'm still a
little lost.

I've a problem regarding reading a stream. I am connected to a port that
sends information using a stream. I can read the information from the stream
with no problem. However, the problem comes when there is nothing to read in
the stream. The stream is still open, so it just hangs on my Read().

EXA:

byte[] buffer = new buffer[2048];
int nBytes
while ((nBytes = this.m_stream.Read(buffer, 0, buffer.Length)) > 0)
{
// Process bytes
}


Well, my problem -- as mentioned -- is here. For example, if the stream
only sends 64 bytes. I will read once, I will process once, and then I will
jump to back to the top of the while-statement where it will Read() again,
but it will just hang here. Should I be doing active monitoring on the
buffer to determine if the EOF string has been received? In my case, the EOT
is three hashmarks: ###. Should I be watching for the three hashmarks and
break out of the loop to prevent hanging?

If I should be watching for the EOT, or ###, should I also watch for the EOT
on a boundary? For example, in one packet it may send ## and the next packet
may contain a solitary #. Or, another example, if I read 2048 of bytes, that
will fill up my entire buffer. The last byte in the buffer may be a #. Then
I read again on the stream and the next two -- and only bytes -- are ##.
Well, this would mean I am at the end. Thank you.

Trecius
 
W

Winfried Wille

Hello Trecius,

you can use a non blocking read by using BeginRead instead of Read, you will
probably find a example for this on google.

Regards
Winfried Wille
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top