title: multiple messages through the same socket doesn't work
body: <p>Im working on a simple ftp server, and the client must send multiples messages to the server, and for each message the server send back to the client a anwser. when the client sends one message it works perfectly and the server responds without any problem, for example, when the client sends "USER username" the server send back to the client "password needed". </p>
  
  <p>But when the client sends another message "PASS password" (using the same socket) it doesnt work ! ONLY the first exchange works (for the username), when the first message is sent, the server anwser without any problem, but it block when it want to send the second message (for the password).</p>
  
  <p>please anyone can help me ? thank you !! </p>
  
  <p>here is my code : </p>
  
  <pre><code> @Test
  public void testProcessPASS() throws IOException{
  
      Socket socket = new Socket(server.getAddress(), server.getcmdPort());
  
      this.ClientReceiveMessage(socket); // to flush
      String cmd = "USER user_test\r\n";
      this.ClientSendMessage(socket, cmd);
      String anwser = this.ClientReceiveMessage(socket); 
      assertEquals("Response error.", Constants.MSG_331.replace("\r\n", ""), anwser);
  
      //PROBLEME STARTS HERE :/
  
      String cmd2 = "PASS pass_test\r\n";
      this.ClientSendMessage(socket, cmd2);
      String anwser2 = this.ClientReceiveMessage(socket); 
      assertEquals(Constants.MSG_230.replace("\r\n", ""), anwser2);
      socket.close();
  
  }
  
  
  public void ClientSendMessage(Socket skt, String msg) throws IOException{
  
      PrintWriter messageClient = new PrintWriter(new OutputStreamWriter(skt.getOutputStream()),true);
      messageClient.println(msg);
      messageClient.flush();
  
  }
  
  public String ClientReceiveMessage(Socket skt) throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
      String res = br.readLine() ;
      return res; 
  }
  </code></pre>
  
  <p>this is the server code : </p>
  
  <pre><code> public class Server implements Runnable {
  
  private ServerSocket cmdserverSocket;
  private ServerSocket dataServerSocket;
  
  private boolean running;
  
  public Server() throws IOException {
      this.cmdserverSocket = new ServerSocket(1024);
      this.dataServerSocket = new ServerSocket(1025);
      this.running = false;
  }
  
  public boolean isRunning() {
      return this.running;
  }
  
  public InetAddress getAddress() {
      return this.cmdserverSocket.getInetAddress();
  }
  
  public int getcmdPort() {
      return this.cmdserverSocket.getLocalPort();
  }
  
  public int getDataPort() {
      return this.dataServerSocket.getLocalPort();
  }
  
  public void run() {
      // TODO Auto-generated method stub
      this.running = true;
      System.out.println("server started on port : " + this.getcmdPort());
  
      while (this.running) {
          try {
              Socket socket = this.cmdserverSocket.accept();
              new Thread(new FtpRequest(socket, this.dataServerSocket))
                      .start();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              System.out.println("server error : " + e.getMessage());
              this.running = false;
          }
      }
  }
  
  }
  </code></pre>
  
  <p>and this is the class that handles client requests and that sends messages to client and running on a new thread : </p>
  
  <pre><code> public class FtpRequest implements Runnable {
  
  private Socket cmdSocket;
  private Socket dataSocket;
  private BufferedReader cmdBufferedReader;
  private DataOutputStream cmdDataOutputStream;
  private ServerSocket dataServerSocket;
  private boolean anonymous;
  private boolean connected;
  private String username;
  private boolean processRunning;
  private String directory;
  
  public FtpRequest(Socket cmds, ServerSocket dts) throws IOException {
  
      this.cmdSocket = cmds;
      this.dataServerSocket = dts;
      this.cmdBufferedReader = new BufferedReader(new InputStreamReader(
              this.cmdSocket.getInputStream()));
      this.cmdDataOutputStream = new DataOutputStream(
              this.cmdSocket.getOutputStream());
      this.anonymous = true;
      this.connected = false;
      this.username = Constants.ANONYMOUS_USER;
      this.processRunning = true;
      this.directory = "/home";
  
  }
  
  /**
   * send a message on the socket of commands
   * 
   * @param msg
   *            the msg to send on the socket of commands
   * @throws IOException
   */
  public void sendMessage(String msg) throws IOException {
      System.out.println("FtpRequest sendMessage : " + msg);
      PrintWriter messageClient = new PrintWriter(new OutputStreamWriter(
              this.cmdDataOutputStream), true);
      messageClient.println(msg);
      messageClient.flush();
  
      /*
       * this.cmdDataOutputStream.writeBytes(msg);
       * this.cmdDataOutputStream.flush(); this.cmdSocket.close();
       */
  }
  
  public void run() {
      // TODO Auto-generated method stub
      System.out.println("FtpRequest running ...");
      try {
          this.sendMessage(Constants.MSG_220); // service ready for new user
          this.handleRequest();
  
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } // service ready for new user
  
  }
  
  /**
   * this method handle the request readen from cmd socket and run the
   * required method
   * 
   * @throws IOException
   */
  private void handleRequest() throws IOException {
  
      String rqst = this.cmdBufferedReader.readLine();
  
      Request request = new Request(rqst);
      System.out.println("FtpRequest handleRequest" + rqst);
  
      switch (request.getType()) {
      case USER:
          this.processUSER(request);
          break;
  
      case PASS:
          this.processPASS(request);
          break;
  
      default:
          this.sendMessage(Constants.MSG_502); // Command not implemented.\r\n
          break;
  
      }
  
      /*
       * if (this.processRunning = true) this.handleRequest();
       * 
       * else { this.cmdSocket.close(); System.out.println("socket closed ");
       * }
       */
  
  }
  
  private void processUSER(Request rqst) throws IOException {
  
      System.out.println("FtpRequest processUSER");
      if (rqst.getArgument().equals(Constants.ANONYMOUS_USER)) {
          this.sendMessage(Constants.MSG_230); // user loged in
          this.connected = true;
          this.anonymous = true;
          this.username = Constants.ANONYMOUS_USER;
      } else if (rqst.getArgument().equals(Constants.USER_TEST)) {
          this.sendMessage(Constants.MSG_331); // User name okay, need
                                                  // password.\r\n
          this.username = Constants.USER_TEST;
      } else
          this.sendMessage(Constants.MSG_332);
  }
  
  private void processPASS(Request rqst) throws IOException {
      System.out.println("FtpRequest processPASS");
      if (rqst.getArgument().equals(Constants.USER_TEST)
              &amp;&amp; rqst.getArgument().equals(Constants.PASS_TEST)) {
          this.sendMessage(Constants.MSG_230);
          this.connected = true;
          this.anonymous = false;
      } else
          this.sendMessage(Constants.MSG_332); // au cas seulement le mot de
                                                  // passe est fourni
  }
  
  
  }
  </code></pre>