C# - StreamReader.ReadLine does not work properly!

33,918

Solution 1

A typical line-reader is something like:

using(StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) {
    string line;
    while((line = reader.ReadLine()) != null) {
        // do something with line
    }
}

(note the using to ensure we Dispose() it even if we get an error, and the loop)

If you want, you could abstract this (separation of concerns) with an iterator block:

static IEnumerable<string> ReadLines(Stream source, Encoding encoding) {
    using(StreamReader reader = new StreamReader(source, encoding)) {
        string line;
        while((line = reader.ReadLine()) != null) {
            yield return line;
        }
    }
}

(note we've moved this into a function and removed the "do something", replacing it with "yield return", which creates an iterator (a lazily iterated, non-buffering state machine)

We would then consume this as simply as:

foreach(string line in ReadLines(Socket.GetStream(), Encoding.UTF8)) {
    // do something with line
}

Now our processing code doesn't need to worry about how to read lines - simply given a sequence of lines, do something with them.

Note that the using (Dispose()) applies to TcpClient too; you should make a habit of checking for IDisposable; for example (still including your error-logging):

using(TcpClient tcpClient = new TcpClient()) {
    try {
       tcpClient.Connect("localhost", serverPort);
       StreamWriter writer = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
       writer.AutoFlush = true;
       writer.WriteLine("login>user,pass");
       writer.WriteLine("print>param1,param2,param3");
    } catch (Exception ex) {
        Console.Error.WriteLine(ex.ToString());
    }
}

Solution 2

The while in your server code is setup to only read one line per connection. You'll need another while within the try to read all of the lines being sent. I think once that stream is setup on the client side, it is going to send all of the data. Then on the server side, your stream is effectively only reading one line from that particular stream.

Share:
33,918
Aleyna
Author by

Aleyna

Updated on November 20, 2020

Comments

  • Aleyna
    Aleyna over 3 years

    Simply I have been trying to implement what BufferedStreamReader does in Java. I have a socket stream open and just want to read it in a line-oriented fashion - line by line.

    I have the following server-code.

    while (continueProcess)
            {
                try
                {
                    StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8);
                    string command = reader.ReadLine();
                    if (command == null)
                        break;
    
                    OnClientExecute(command);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
    

    And the following client-code:

    TcpClient tcpClient = new TcpClient();
            try
            {
                tcpClient.Connect("localhost", serverPort);
                StreamWriter writer = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
                writer.AutoFlush = true;
                writer.WriteLine("login>user,pass");
                writer.WriteLine("print>param1,param2,param3");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                tcpClient.Close();
            }
    

    The server reads only the very first line (login>user,pass) and then ReadLine returns null!

    What's the easiest way of achieving this line-oriented reader as it is in Java's BufferedStreamReader? :s

  • Aleyna
    Aleyna over 14 years
    After altering my server and client code as follows server: using (StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) { while (continueProcess) .... client: ... Thread.Sleep(10000); writer.WriteLine("print>test,one,two"); I inserted Thread.Sleep(10000) to my client code and server started throwing exception. The client socket will be open till client exits and since client will send data throughout its lifecycle seems like this code will throw error. Don't you think so?
  • LJM
    LJM over 14 years
    Without seeing what the new code or new exception is, I'm not sure what the problem might be.
  • Masud Rahman
    Masud Rahman over 8 years
    When I use StreamReader for reading from the socket, I found that the client program is freezing, taking infinite amount of time at ReadLine() method, and finally failing to read.
  • Marc Gravell
    Marc Gravell over 8 years
    @MasudRahman that usually means either: the server isn't sending a complete line (i.e. you're asking it to wait for something that the server doesn't have a reason to send you), or the server has forgotten to flush an output buffer. You can add a read timeout, but ultimately it can't make the server send data. You can of course do the reading and buffering yourself, but this just changes the nature of the problem - it can then be your job to decide that a complete line hasn't been sent after some period of time.