title: Java, Sockets, BufferedReader and StringBuilder
body: <p>Yestarday I wrote a post about Java and Sockets, and today I'm still here because I'm having an issue with BufferedReaders.<br />
  I searched some questions here in StackOverflow and I understand the problem, but I can't fix it <br />
  My "application" has got two parts: a server and a client, and the scope of the application is to execute MS-DOS commands on the machine where the server is running (the commands are sent by the client).<br/>
  Now the code (I will post the total code because it's easier to understand, I will put a comment in non-working part of the code)<br /> <br /> Server: <br /> <br /></p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class TCPCmdServer {
      public int port;
      public ServerSocket server;
      public final String version = "Beta 1.0";
  
      TCPCmdServer(int port) {
          this.port = port;
          if (!createServer())
              System.out.println("Cannot start the server");
          else {
              System.out.println("**********************************************");
              System.out.println("Command executer, server version: " + version);
              System.out.println("Server running on port " + port);
              System.out.println("Code by luc99a alias L99");
              System.out.println("**********************************************");
          }
      }
  
      public boolean createServer() {
          try {
              server = new ServerSocket(port);
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
  
          return true;
      }
  
      public static void main(String[] args) {
          TCPCmdServer tcp = new TCPCmdServer(5000);
  
          while (true) {
  
              Socket socket = null;
              BufferedReader in = null;
              BufferedWriter out = null;
  
              try {
                  socket = tcp.server.accept();
                  System.out.println("A client has connected");
                  in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                  out.write("Welcome on the server... type the commands you like, type END to close the connection\n");
                  out.flush();
              } catch (IOException exc) {
                  exc.printStackTrace();
              }
  
              if (socket != null &amp;&amp; in != null &amp;&amp; out != null) {
                  try {
                      String cmd = null;
                      while (!(cmd = in.readLine()).equals("END")) {
                          System.out.println("Recieved: " + cmd);
                          Process p = Runtime.getRuntime().exec(cmd);
                          BufferedReader pRead = new BufferedReader(new InputStreamReader(p.getInputStream()));
  
                          String line;
                          StringBuilder builder = new StringBuilder();
                          while ((line = pRead.readLine()) != null) {
                              builder = builder.append(line + "\n");
                          }
                          out.write(builder.toString() + "\n");
                          //here is sent "EnD"
                          out.write("EnD \n");
                          out.flush();
                          System.out.println(builder.toString());
                          pRead.close();
                      }
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  } finally {
                      System.out.println("Closing connection...");
                      try {
                          socket.close();
                          in.close();
                          out.close();
                      } catch (IOException excp) {
                          excp.printStackTrace();
                      }
                  }
              }
          }
  
      }
  }
  </code></pre>
  
  <p>And now the code for the client part <br /></p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class TCPCmdClient {
      public Socket socket;
      public int port;
      public String ip;
      public final String version = "Beta 1.0";
  
      TCPCmdClient(String ip, int port) {
          this.ip = ip;
          this.port = port;
          if (!createSocket())
              System.out.println("Cannot connect to the server. IP: " + ip + " PORT: " + port);
          else {
              System.out.println("**********************************************");
              System.out.println("Command executer, client version: " + version);
              System.out.println("Connected to " + ip + ":" + port);
              System.out.println("Code by luc99a alias L99");
              System.out.println("**********************************************");
          }
      }
  
      public boolean createSocket() {
          try {
              socket = new Socket(ip, port);
          } catch (IOException e) {
              return false;
          }
  
          return true;
      }
  
      public static void main(String[] args) {
          TCPCmdClient client = new TCPCmdClient("127.0.0.1", 5000);
  
          try {
              BufferedReader sysRead = new BufferedReader(new InputStreamReader(System.in));
              BufferedReader in = new BufferedReader(new InputStreamReader(client.socket.getInputStream()));
              BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.socket.getOutputStream()));
  
              String response = in.readLine();
              System.out.println("Server: " + response);
  
              boolean flag = true;
              while (flag) {
                  System.out.println("Type a command... type END to close the connection");
                  String cmd = sysRead.readLine();
                  out.write(cmd + "\n");
                  out.flush();
                  if (cmd.equals("END")) {
                      client.socket.close();
                      sysRead.close();
                      in.close();
                      out.close();
                      flag = false;
                  } else {
                      //The loop doesn't finish because the reader
                      //listens for a new line
                      //so I used the string "EnD", sent by the server to
                      //stop the loop, anyway it doesn't seem to work
                      //I put a comment in the server where "EnD" is sent
                      String output;
                      while (((output = in.readLine()) != null)) {
                          if (output.equals("EnD")) {
                              break;
                          } else {
                              System.out.println(output);
                          }
                      }
  
                      System.out.println("   ***************************************   ");
                  }
              }
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><br />
  The problem is that the BufferedReader waits for a new line forever in the while loop (I wrote a comment in the code). I tryed to stop it using a "special string", but it doesn't seem to work. <br />
  <br />
  I can't change the while in <br /></p>
  
  <pre><code>String output;
  while (((output = in.readLine()) != null) &amp;&amp; output.length &gt; 0)
  {
     //code here...
  }
  </code></pre>
  
  <p>because in the output of the MS-DOS command (think on "ipconfig") are also present empty lines. <br />
  How could I correct it? <br />
  Thank you for your help!</p>