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 8 17325
Scott <sc***@discussi ons.microsoft.c om> 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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***@discussi ons.microsoft.c om> 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too .
Use the DataAvailable property on NetworkStream.
kevin aubuchon
"Scott" <Sc***@discussi ons.microsoft.c om> 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***@discussi ons.microsoft.c om> 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too .
Scott <Sc***@discussi ons.microsoft.c om> 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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***@discussi ons.microsoft.c om> 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too .
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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(da ta);
string str = "";
str = reader.ReadLine ();
while( str != null)
{
Console.WriteLi ne(str);
str = reader.ReadLine ();
}
data.Close();
}
catch(WebExcept ion 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.
Jell-O Biafra <jb*****@dead.k ennedies> 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(da ta); string str = ""; str = reader.ReadLine ();
while( str != null) { Console.WriteLi ne(str); str = reader.ReadLine (); } data.Close(); } catch(WebExcept ion exp) { MessageBox.Show (exp.Message, "Exception" ); }
Well, I'd use:
string str;
while ( (str=reader.Rea dLine()) != 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 pressing one of them it
triggers a specific query, like getting the server uptime, date or who (is
online). The problem is that when I press one of the buttons it send the
query to the server, reads from the BufferedReader. This goes all well but...
|
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 thread hangs when it reaches
the end of the stream.
|
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 be really useful is to be able to
use the server as a sort of cmd prompt remote control. I use the same
sort of code below
I can write a cmd to the popen2 cmd but when reading them it reads but
then
hangs on the readline there isn't a end of...
|
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 written to the output stream.
We are willing to pay $ to any expert (e.g. MVP) consultant who has to time
to help us track down this problem.
(we will discuss rates if you can prove your expertise, and problem solving
approach).
|
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 commandline app, it will hang when it
comes to the command-prompt (there's no return to send it to readline).
Here's the code:
public bool RunCommands(string cmds, ArrayList files)
{
| |
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.
while(true){
inputLine = reader.ReadLine ();
if ( inputLine != null )
|
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;
|
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
elegant way to inject CrLf into the input stream, which effectively
unblocks
the ReadLine method. Last night, I had finally got the
WriteConsoleInput
|
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 line Count of Sr
}
i just want to get a count of sr.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |