title: Java, problem sending a message with socket connection
body: <p>I have a problem with my Java program. It has a socket connection between a server and many client. Here is the server (the part which concerns the problem):</p>
  
  <pre><code>private static ArrayList&lt;ParallelServer&gt; clientConnected = new ArrayList&lt;ParallelServer&gt;();
  
  
  public Server(int port) {
      this.port = port;
      if (!startServer())
          JOptionPane.showMessageDialog(new JFrame(""),
                  "Error!", "ERROR!",
                  JOptionPane.ERROR_MESSAGE);
  }
  
  private boolean startServer() {
      try {
          server = new ServerSocket(port);
          loadDatabase();
      } catch (IOException ex) {
          ex.printStackTrace();
          return false;
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      return true;
  }
  
  public void runServer() {
      while (true) {
          try {
              client = server.accept();
              ParallelServer pServer = new ParallelServer(client);
              clientConnected.add(pServer);
              Thread thread = new Thread(pServer);
              thread.start();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  }
  
  
  public static void sendBroadcast(String username) throws IOException {
      for(int i = 0; i &lt; clientConnected.size(); i++)
          clientConnected.get(i).sendAnswer("@change," + username);
  }
  </code></pre>
  
  <p>The parallel server is: </p>
  
  <pre><code>    private Socket client;
      private InputStreamReader inputstreamreader;
      private BufferedReader bufferedreader;
      private PrintWriter printwriter;
  
      public ParallelServer(Socket client) {
          this.client = client;
      }
  
      public void run() {
          try {
              inputstreamreader = new InputStreamReader(client.getInputStream());
              bufferedreader = new BufferedReader(inputstreamreader);
              printwriter = new PrintWriter(client.getOutputStream(), true);
              String lineread = "";
  
              while (client.isConnected()) {
                  lineread = bufferedreader.readLine();
                  doCommand(lineread);
              }
          } catch (UnknownHostException unhe) {
          } catch (InterruptedIOException intioe) {
          } catch (IOException ioe) {
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }
      }
  
      public void sendAnswer(String answer) throws IOException {
          printwriter = new PrintWriter(client.getOutputStream(), true);
          printwriter.println(answer);
          printwriter.flush();
      }
  </code></pre>
  
  <p>And here is the client:</p>
  
  <pre><code>private String serverurl = "localhost";
  private int serverport = 7777;
  private PrintWriter printwriter;
  private InputStreamReader inputstreamreader;
  private BufferedReader bufferedreader;
  private Socket server;
  
  public Client() {
      server = null;
      try {
          server = new Socket(serverurl, serverport);
          server.setSoTimeout(5000);
      } catch (UnknownHostException unhe) {
          System.out.println("UnknownHostException: " + unhe.getMessage());
      } catch (InterruptedIOException intioe) {
          System.out.println("Timeout while attempting to establish socket connection.");
      } catch (IOException ioe) {
          JOptionPane.showMessageDialog(new JFrame(),"Unable to reach the server!","ERROE!",JOptionPane.ERROR_MESSAGE);
      }
  }
  
  public String sendCommand(String command) throws IOException {
      if(server == null) {
          try {
              server = new Socket(serverurl, serverport);
              server.setSoTimeout(5000);
          } catch (UnknownHostException unhe) {
              System.out.println("UnknownHostException: " + unhe.getMessage());
          } catch (InterruptedIOException intioe) {
              System.out.println("Timeout while attempting to establish socket connection.");
          } catch (IOException ioe) {
              JOptionPane.showMessageDialog(new JFrame(),"Unable to reach the server!","ERROR!",JOptionPane.ERROR_MESSAGE);
          }
      }
      if(server != null) {
          printwriter = new PrintWriter(server.getOutputStream(), true);
          printwriter.println(command);
          printwriter.flush();
          inputstreamreader = new InputStreamReader(server.getInputStream());
          bufferedreader = new BufferedReader(inputstreamreader);
  
          return bufferedreader.readLine();
      }
      else
          return "@serverProblem";
  }
  </code></pre>
  
  <p>The program is a simple online game with turns. Players' turns are created with a queue and when a player passes his turn, the server send a broadcast message which say "Now it is 'Player 1' turn." (for instance). My problem is that when a client receive the message, its like it add the answer "Now it is 'Player 1' turn." to the next message it will receive. In my case: when a player passes his turn, he sends "@passTurn,username". The ParallelServer class polls it from the queue, puts it at the bottom of the queue, sends the client "@ok" to tell it that the turn has changed successfully and tells the Server class to send the broadcast message. Then, when the same client will try do do a further action, it will consider "Now it is 'Player 1' turn." as the answer the server has given to it. Instead, I would like that the server and the clients work as always and when the broadcast message is cought, the client is notified without any collateral effect.
  What can I do?
  Thanks.</p>