C# StreamReader.ReadLine Problem

  • Thread starter Thread starter IDandT
  • Start date Start date
I

IDandT

Hi,
I am writing a small program that connect to IRC. I create
a TCPClient connection and i use stream to read and write
data. It works ok, but i have a problem reading from stream.

while(true){

inputLine = reader.ReadLine ();

if ( inputLine != null )
{
....
....
}
}

If the read stream is empty the program wait at ReadLine
( i think that is waiting for a new data, but also i think that it should
return -1). I need that it not stops at ReadLine. Any idea?

thanks

IDandT
 
Hi IDandt,

StreamReader isn't really meant to do network streams because you can't know how much of the stream is available when you try to read from it. It may work, but I wouldn't trust it. Use a basic stream instead.
 
IDandT,

Like Morten stated, the with network streams, you really don't know when
the end of the stream is. If you are connected over TCP, then your socket
will hang while it awaits new data.

Morten also recommends that you not use StreamReader. To be more
specific, you don't use it because it buffers the stream in the reader, and
will always try and read beyond what you tell it (the minimum buffer size is
128 bytes).

What I would do is actually derive a class from TextReader which will
perform the encoding that you want without the buffering. It would
encapsulate your code better, and you can easily swap it out for another
reader, and even pass it along to other readers that could use it (for
whatever reason).

Hope this helps.
 
In addition to the other posts I will add that you will have to implement
either an async network connection or use a worker thread to wait for the
data to be available, you cannot predict in advance when the next chunk of
data will arrive .

Cheers,
 
Back
Top