473,473 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ReadLine from a network stream hangs if it is empty

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
Nov 16 '05 #1
8 17287
Scott <sc***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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

-----Original Message-----
Scott <sc***@discussions.microsoft.com> wrote:
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*. Itshould 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, eitherby stating in advance how much there is to read, or by indicating the"end of data". Alternatively, if the client isn't expected to doanything else afterwards, the server can close the socket.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 16 '05 #3
Use the DataAvailable property on NetworkStream.

kevin aubuchon
"Scott" <Sc***@discussions.microsoft.com> wrote in message
news:3d****************************@phx.gbl...
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

-----Original Message-----
Scott <sc***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 16 '05 #4
Scott <Sc***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5


while(true)
{

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

if(bytes < buffer.Length)
{
break;
}
}
Scott wrote:
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

-----Original Message-----
Scott <sc***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.


Nov 16 '05 #6
Scrubbing Bubbles <sc****************@scrubbing.bubbles> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

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");
}
Jon Skeet [C# MVP] wrote:
Scrubbing Bubbles <sc****************@scrubbing.bubbles> wrote:
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.


Nov 16 '05 #8
Jell-O Biafra <jb*****@dead.kennedies> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
Hi I'm doing a project for school and wrote an applet that makes a socket connection to a server (smae host as webserver) that was setup for this project. In the applet there are 3 buttons and by...
1
by: Rasmusson, Lars | last post by:
Hi, I have a problem doing I/O in a python thread. I want a thread to execute a command using os.popen, read its input until the end, and then print 'DONE' and terminate. However, the...
0
by: Guy | last post by:
Ok this is might take some exsplaining as this is just example code, I have a telnet server which I've created, its ment to be a a process queue control thing. One of the things I think that would...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
1
by: JC | last post by:
I'm trying to create a GUI wrapper for dumpbin, and so I'm using the Process class to run the command-line application. The problem is that if I use Readline() to get the output from the...
4
by: IDandT | last post by:
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. ...
1
by: Frosty Madness | last post by:
I have the beginnings of a device emulator that sits on the network... ************** using System; using System.Threading; using System.Net; using System.Net.Sockets; using System.Text;
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
4
by: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.