473,387 Members | 1,844 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,387 software developers and data experts.

ReadLine problem

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:

string file = "";
string line;
while ((line = read.ReadLine())!= null)
{
file += "\r\n"+line;
}

When I am debug, the code reads till the last line in the network stream. When it attempts to read the last line, debug fails, and interestingly, the window of the form (the lines given above are in the class Form1) appears. Pressing F10, F11 or F5 does not work. In fact they are greyed out in the Debug menu, but Stop Debug, etc still functions.

I think a simple problem but I could not figure it out.

Thank you for helping,

Halil
Nov 19 '08 #1
10 2238
r035198x
13,262 8TB
1.) I would expect the compiler to whine and say that line may not have been initialized (or similar).
2.) Better use StringBuilder when concatenating strings in a loop. It's more efficient.
3.) What happens when you run the code without any break points?
Nov 19 '08 #2
Plater
7,872 Expert 4TB
I would guess that the debug is not broken, but rather that you are not using readline() correct.
ReadLine() is a blocking call so long as their is no "end of stream", which there wouldn't be on a network stream that is still "connected"/"open".
So your debug is stuck sitting there waiting at the ReadLine() command.
If you spaced out your looping mechanism to make it "long winded", you would be able to do step-by-step execution and see that it sits there on the ReadLine() statement.
You will need to code up additional precautions against this.
Nov 19 '08 #3
r035198x
13,262 8TB
..
ReadLine() is a blocking call so long as their is no "end of stream", which there wouldn't be on a network stream that is still "connected"/"open".
...
Missed that one completely.
Nov 19 '08 #4
ReadLine() is a blocking call so long as their is no "end of stream", which there wouldn't be on a network stream that is still "connected"/"open".
Thanks, I understood the reason why it stucks there.

After reading this, I tried another way.

Expand|Select|Wrap|Line Numbers
  1. int length = (int)read.BaseStream.Length;
  2. Byte[] fileByte = new Byte[length];
  3. read.BaseStream.Read(fileByte, 0, length);
  4. file += Encoding.ASCII.GetString(fileByte);
When debugger comes to the first line above, an exception is thrown. It says "The stream does not support seek operations".

So, I can not use ReadLine() to read till the end of the file, and also can not use Read() because seek operations are not supported. Do you have any other suggestions about reading the network stream?
Nov 19 '08 #5
Plater
7,872 Expert 4TB
You can use read, it's the .Length that you cannot use. Since the stream is still open, there is no "end".
The seek comment was because for .Length to be computed, it needs to read till the end and then come back to the start (using Seeking)
Nov 19 '08 #6
OK, I can use read, but how will I know the length of the file? You know, it requires the number of the bytes to be read as the third parameter. I can give a fixed value but it would not be a good solution.
I feel like reinventing the wheel, how can a file transfer be this hard? :)
Nov 19 '08 #7
Plater
7,872 Expert 4TB
Well what protocol are you using to transfer this file? And if it is your own, then you need to create a method for knowing how big the file will be.
Nov 19 '08 #8
I am using the Tcp Protocol.

Expand|Select|Wrap|Line Numbers
  1. listen = new TcpListener(System.Net.IPAddress.Any, Convert.ToInt16(textBox1.Text));
  2. listen.Start();
  3. t = new Thread(new ThreadStart(ReadingThread));
  4. t.Start();            
inside ReadingThread I use a socket, and assign a network stream to it, and finally assign a stream reader to the stream.
Expand|Select|Wrap|Line Numbers
  1. socket = listen.AcceptSocket();
  2. netStr = new NetworkStream(socket);                
  3. read = new StreamReader(netStr);
  4.  
Nov 19 '08 #9
Plater
7,872 Expert 4TB
Ok, so you are making up your own file transfering protocol.
So start it with a header that gives file size, perhaps like

TRANSMISIONSTART
FILETRANSFERHEADER
FILENAME:myfile.tmp
FILESIZE:1024
ENDHEADER


And then you would put 1024 bytes following it that would make up the file size.

Your receive end can read the command header and go ok i read 1024 bytes after the ENDHEADER marker
Nov 19 '08 #10
It really helped.

Thanks
Nov 19 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT...
1
by: Jian Qiu | last post by:
Hi, I tried to install python2.4.2. Unfortunately building 'readline' extension failed. Here is what I got: (It is a bit long. If you are impatient, please look at the end where it reports the...
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...
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...
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 08:39, Akira Kitada wrote: Please post a bug report on python.org about these failures. The multiprocessing module is still fairly new and obviously needs more fine tuning for...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.