Stream Reading -- Again

  • Thread starter Thread starter Trecius
  • Start date Start date
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
 
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
 
Back
Top