title: Chat: Sending message to all clients on a server
body: <p>I have a server-client application set. (HOMEWORK)</p>
  
  <p>So far I have figured out how to have multiple clients connect to the server and have the server aggregate the messages the clients send, as well as having the server send the client's message back to the client and displaying it in the chat pane.</p>
  
  <p>My issue is trying to send a message to multiple clients. <strong>I am only allowed to use ServerSocket and Socket libraries.</strong></p>
  
  <p>Say I have 2 clients connected to the server. One client sends a message, the message is displayed in the client's chat. The second client sends a message, the first client does NOT receive it, and the second client's chat window displays the first client's message.</p>
  
  <p>Essentially the server is sending the most recent message that the respective client has not displayed in the chatbox, and I have no idea why or how. </p>
  
  <p>Code for server-to-client communication :</p>
  
  <pre><code>Class CommunicationThread extends Thread {
  
    //Vector containing all client sockets currently connected
    //Held offsite, here for clarity
    public Vector&lt;Socket&gt; socketVector;
  
    public CommunicationThread (Socket clientSoc, Server ec3, Vector&lt;Socket&gt;socketVectorg)
    {
      //add new socket to vector, start thread
      clientSocket = clientSoc;
      socketVectorg.add(clientSocket);
      this.socketVector = socketVectorg;
      gui = ec3;
    }
  
    public void run()
    {
      System.out.println ("New Communication Thread Started");
  
      try {
          //Client's chat box (output)
          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
                  true);
  
          //Input line from client
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(clientSocket.getInputStream()));
  
          String inputLine;
  
          while ((inputLine = in.readLine()) != null) {
              System.out.println("Server: " + inputLine);
              gui.history.insert(inputLine + "\n", 0);
  
              //*************HERE IS MY ISSUE*******************
              for(Socket s : socketVector){
                  out = new PrintWriter(s.getOutputStream(),
                          true);
                  out.println(inputLine);
              }
  
              if (inputLine.equals("Bye."))
                  break;
  
              if (inputLine.equals("End Server."))
                  gui.serverContinue = false;
  
          }
  
          out.close();
          in.close();
          clientSocket.close();
      } 
      catch (IOException e) 
      {
       System.err.println("Problem with Communication Server");
       //System.exit(1); 
      } 
    }
  } 
  </code></pre>
  
  <p>I know I am overwriting "out" but I don't think that is my issue so it is in my code while I am testing.</p>
  
  <p>My issue is marked in the code above. The vector accurately stores the socket ID, and since I am creating a new PrinterWriter based on the vector, I would only assume that it would get the output field of the respective client, but it does not. </p>
  
  <p>My intuition is that it is a problem with threading or closing the output, but I honestly don't know. </p>
  
  <p>Any recommendations are appreciated.</p>