473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# StreamReader.ReadLine Problem

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
Nov 16 '05 #1
4 18245
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"IDandT" <vi******@wanadoo.es> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #3
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"IDandT" <vi******@wanadoo.es> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #4
Now it works ok, very thanks to all.

"IDandT" <vi******@wanadoo.es> escribió en el mensaje
news:eO**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #5

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

Similar topics

5
by: Andy Mee | last post by:
Hello one and all, I'm developing an Asp.NET system to take a CSV file uploaded via the web, parse it, and insert the values into an SQL database. My sticking point comes when I try to split()...
3
by: VMI | last post by:
When I execute a ReadLine from an ascii file with special chars (ie. the 'Ñ' in "NUÑEZ PEREZ"), it automatically deletes this character. So "NUÑEZ PEREZ" becomes "NUEZ PEREZ". How can this be...
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
1
by: Henry | last post by:
Hi, reading a textfile with the ReadLine method of a Streamreader objects the resulting string does not contain the special characters from the source file: e.g. Paragraph § and all the umlauts...
2
by: Kin | last post by:
I am trying to read the output of an external application using redirected stdout and StreamReader::ReadLine(). The problem is that ReadLine() blocks and I am either reading nothing or just part...
2
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
2
by: Thelonious Monk | last post by:
I have a problem where some data is being eliminated. The problem is that the data contains signed numeric fields (the low-order byte of a negative number uses the first 4 bits as a sign and...
2
by: jedicks | last post by:
Hi I am new to c#. I hava a problem which i am struggling to solve. Please look at code below: StreamReader sr = new StreamReader(sFilename); string line = sr.ReadLine(); ...
10
by: HalilTorun | last post by:
Hello, I am having a trouble with the ReadLine method in C#. Basically I have a StreamReader (read) attached to a NetworkStream. No problem with this part. While reading I use the lines below:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.