ReadLine from a network stream hangs if it is empty

  • Thread starter Thread starter Scott
  • Start date Start date
S

Scott

Hi guys,

If I try to call read(), readline(), readtoend() and
there is nothing to read (from a never ending loop for
example) the program seems to continue but it exits the
loop for no apparent reason.

We also can't check the stream for the length, as the
network stream doesn't support seek operations

MSDN reckons that the functions should return null if
there is nothing to read but it doesn't.

Can anybody post some example code that shows how to read
the entire contents of a network stream and handling if
there isn't anything in it?

Thanks in advance,


Scott
 
Scott said:
If I try to call read(), readline(), readtoend() and
there is nothing to read (from a never ending loop for
example) the program seems to continue but it exits the
loop for no apparent reason.

We also can't check the stream for the length, as the
network stream doesn't support seek operations

MSDN reckons that the functions should return null if
there is nothing to read but it doesn't.

No, ReadLine should return null if the stream has been *closed*. It
should block if there's just no data ready.
Can anybody post some example code that shows how to read
the entire contents of a network stream and handling if
there isn't anything in it?

Your protocol should indicate when all the data has been read, either
by stating in advance how much there is to read, or by indicating the
"end of data". Alternatively, if the client isn't expected to do
anything else afterwards, the server can close the socket.
 
Hi Jon,

Thanks for the info, I guess my real problem is that I
don't know what the correct method is to check if there
is any data in the stream without causing it to screw up
(if there is no data ready)

Is there a way to check if readline has anything in it
before trying to get data from it? because currently if
we try and get the contents of readline() into a variable
and readline is empty, it exits our 'never ending' loop
and processing stops completely, but we dont get any
errors thrown at us

Cheers,

Scott
 
Scott said:
Thanks for the info, I guess my real problem is that I
don't know what the correct method is to check if there
is any data in the stream without causing it to screw up
(if there is no data ready)

Is there a way to check if readline has anything in it
before trying to get data from it? because currently if
we try and get the contents of readline() into a variable
and readline is empty, it exits our 'never ending' loop
and processing stops completely, but we dont get any
errors thrown at us

As Kevin says, you can use DataAvailable - but then you can easily read
to the point where that will return false even though there's more data
which is *going* to be sent. As I say, the *protocol* should indicate
whether or not there should be any data in the stream.
 
while(true)
{

int bytes = cSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);

if(bytes < buffer.Length)
{
break;
}
}
 
Scrubbing Bubbles said:
while(true)
{

int bytes = cSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);

if(bytes < buffer.Length)
{
break;
}
}

No, that's no good - there's no guarantee that a network stream will
return data filling the whole buffer even if there's then going to be
data available in a moment.
 
How about this?

// Address of URL
string URL = textBox1.Text;
try
{
// Get HTML data
WebClient client = new WebClient();
Stream data = client.OpenRead(URL);
StreamReader reader = new StreamReader(data);
string str = "";
str = reader.ReadLine();


while( str != null)
{
Console.WriteLine(str);
str = reader.ReadLine();
}
data.Close();
}
catch(WebException exp)
{
MessageBox.Show(exp.Message, "Exception");
}
 
Jell-O Biafra said:
How about this?

// Address of URL
string URL = textBox1.Text;
try
{
// Get HTML data
WebClient client = new WebClient();
Stream data = client.OpenRead(URL);
StreamReader reader = new StreamReader(data);
string str = "";
str = reader.ReadLine();


while( str != null)
{
Console.WriteLine(str);
str = reader.ReadLine();
}
data.Close();
}
catch(WebException exp)
{
MessageBox.Show(exp.Message, "Exception");
}

Well, I'd use:

string str;

while ( (str=reader.ReadLine()) != null)
{
...
}

myself, but yes, that's the gist of it. (I'd also use using statements
to close the reader/stream, btw.)

However, that then gets back to the OP's problem of ReadLine blocking
when there's no more data available but the stream hasn't been closed
by the other end.
 
Back
Top