title: Cannot send commands via Sockets
body: <p>I'm new to the world of Java and now I'm trying to create a socket program. I created a server and a client, but they didn't seem to work. Now I post the code.</p>
  
  <p>This is the server:</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class TCPCmdServer 
  {
  public int port;
  public ServerSocket server;
  
  TCPCmdServer (int port)
  {
      this.port = port;
      if(!createServer())
          System.out.println("Cannot start the server");
      else System.out.println("Server running on port " + port);
  }
  
  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);
  
      boolean flag = true;
      while (flag)
      {
          try
          {
              Socket socket = tcp.server.accept();
              System.out.println("A client has connected");
              BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
              out.write("Welcome on the server... type the commands you like, type END to close me\n");
              out.flush();
  
              String cmd = in.readLine();
              System.out.println("Recieved: " + cmd);
              if (cmd.equals("END"))
              {
                  System.out.println("Shutting down server...");
                  socket.close();
                  in.close();
                  out.close();
                  flag = false;
              }
              else
              {
                  Process p = Runtime.getRuntime().exec(cmd);
                  BufferedReader pRead = new BufferedReader(new InputStreamReader(p.getInputStream()));
  
                  String line;
                  while ((line = pRead.readLine()) != null)
                  {
                      System.out.println(line);
                      out.write(line + "\n");
                      out.flush();
                  }
              }
          }
          catch (IOException ex)
          {
              ex.printStackTrace();
          }
      }
  }
  }
  </code></pre>
  
  <p>And this is the client:</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class TCPCmdClient 
  {
  public Socket socket;
  public int port;
  public String ip;
  
  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("Connected to " + ip + ":" + port);
  }
  
  public boolean createSocket ()
  {
      try
      {
          socket = new Socket(ip, port);
      }
      catch (IOException e) 
      {
          e.printStackTrace(); 
          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 server");
              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
              {
                  String outputline;
                  while ((outputline = in.readLine()) != null)
                      System.out.println(outputline);
              }
          }
      }
      catch (IOException ex)
      {
          ex.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>[old]
  I believe the problem is with the input and output streams, but I can't understand why they don't work. </p>
  
  <p>The expected behavior is as follows: The client connects to the server then the server send response. The client asks the user to insert a MS-DOS command (or a "END" command), the command is then sent to the server. The server executes the command on the computer where it is running (in case the command is END it closes the connection). Then the server sends the result of the command to the client, and the client displays it to the user.
  [/old]</p>
  
  <p>Now the only problem is that I have to close and re-open a client any time I like to execute a new command</p>