ReadLine problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HalilTorun
    New Member
    • Nov 2008
    • 7

    #1

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Plater
        ..
        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.

        Comment

        • HalilTorun
          New Member
          • Nov 2008
          • 7

          #5
          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.

          Code:
          int length = (int)read.BaseStream.Length;
          Byte[] fileByte = new Byte[length];
          read.BaseStream.Read(fileByte, 0, length);
          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?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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)

            Comment

            • HalilTorun
              New Member
              • Nov 2008
              • 7

              #7
              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? :)

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                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.

                Comment

                • HalilTorun
                  New Member
                  • Nov 2008
                  • 7

                  #9
                  I am using the Tcp Protocol.

                  Code:
                  listen = new TcpListener(System.Net.IPAddress.Any, Convert.ToInt16(textBox1.Text));
                  listen.Start();
                  t = new Thread(new ThreadStart(ReadingThread));
                  t.Start();
                  inside ReadingThread I use a socket, and assign a network stream to it, and finally assign a stream reader to the stream.
                  Code:
                  socket = listen.AcceptSocket();
                  netStr = new NetworkStream(socket);                
                  read = new StreamReader(netStr);

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Ok, so you are making up your own file transfering protocol.
                    So start it with a header that gives file size, perhaps like

                    TRANSMISIONSTAR T
                    FILETRANSFERHEA DER
                    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

                    Comment

                    • HalilTorun
                      New Member
                      • Nov 2008
                      • 7

                      #11
                      It really helped.

                      Thanks

                      Comment

                      Working...