title: Communication failure between Server and clients in the multiplayer version of a game
body: <p>I am showing you two classes that are part of a larger game. The first class builds a GUI (A multiplayer lounge) that shows the players joining the game. When a player hits the &quot;Leave&quot; button I want the client to send a &quot;remove&quot; message to the server, that removes the client from the server record of active players. Following this the server will forward a &quot;remove&quot; message to each client so that the quitter is removed from each clients list of players.</p>
  <p>When the &quot;Leave&quot; button is pressed the remove message is successfully sent to the server. However the other players do not get the remove message forwarded to them. I cant figure out why. Any suggestions?</p>
  <pre><code>import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.Socket;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.SwingConstants;
  
  
  public class BattleArenaLounge {
  
      JFrame theFrame1;
      JTextArea players;
      Character Avatar;
      
      BufferedReader reader;
      PrintWriter writer;
      Socket sock;
      
      
      public void startUp() {
          
          Avatar = new Character();
          buildGUI();
      }
      
      public void buildGUI() {
          
          theFrame1 = new JFrame(&quot;Battle Arena&quot;);
          //theFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          GridBagLayout layout = new GridBagLayout();
          GridBagConstraints c = new GridBagConstraints(); 
          
          JPanel background1 = new JPanel(layout);
          background1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
          
          JPanel Buttons = new JPanel(layout);
          Buttons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
          
          ////////////////////////////////////////////////Welcome text
          JLabel Welcome = new JLabel(&quot;&lt;html&gt;Hello &quot;+Avatar.getName()+&quot;. Welcome to the multiplayer lounge.&lt;/html&gt;&quot;,SwingConstants.CENTER);
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx=0;
          c.gridy=0;
          background1.add(Welcome,c);
          
          ////////////////////////////////////////////////Player list
          players = new JTextArea(10,50); 
          players.setLineWrap(true);
          players.setWrapStyleWord(true);
          players.setEditable(false);
          JScrollPane playerArea = new JScrollPane(players);
          
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx=0;
          c.gridy=1;
          background1.add(playerArea,c);
          
          ///////////////////////////////////////////////buttons
          JButton Opponant = new JButton(&quot;Choose!&quot;);                  
          Opponant.addActionListener(new MyChooseListener());
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx=0;
          c.gridy=0;
          Buttons.add(Opponant,c);
          
          JButton Leave = new JButton(&quot;Leave&quot;);                   
          Leave.addActionListener(new MyLeaveListener());
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx=0;
          c.gridy=1;
          Buttons.add(Leave,c);
          
          
          c.fill = GridBagConstraints.VERTICAL;
          c.gridx=1;
          c.gridy=1;
          background1.add(Buttons,c);
          
          //////////////////////////////////////////////Set frame
          theFrame1.getContentPane().add(background1);
          theFrame1.setBounds(50,50,300,300);
          theFrame1.pack();
          theFrame1.setVisible(true);
          
          /////////////////////////////////////////////Networking
          setUpNetworking1();
          
          Thread readerThread = new Thread(new IncomingReader());
          readerThread.start();
          
          try{
              writer.println(Avatar.getName()+&quot;,join,&quot;+Avatar.getCharID());
              writer.flush();
              
          }catch(Exception ex){
              ex.printStackTrace();
          }
          
      }
  ////////////////////////////////////////////////////Listeners
      public class MyChooseListener implements ActionListener{
          public void actionPerformed(ActionEvent a){
          }
      }
      
      public class MyLeaveListener implements ActionListener{
          public void actionPerformed(ActionEvent a){
              
              writer.println(Avatar.getName()+&quot;,remove&quot;);
              System.out.println(&quot;removeID sent: &quot;+Avatar.getName()+&quot;,remove&quot;);
              //try {
              //  Thread.sleep(1500);
              //} catch (InterruptedException e) {
              //  e.printStackTrace();
              //}
              //theFrame1.dispose();
          }
      }
      
  ///////////////////////////////////////////////////Set up connection to server
      private void setUpNetworking1(){
  
          try{
              sock = new Socket(&quot;127.0.0.1&quot;, 5000);
  
              InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
              reader = new BufferedReader(streamReader);
              
              writer = new PrintWriter(sock.getOutputStream());
              //writer = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
  
              System.out.println(&quot;Networking established.&quot;);
  
          }catch(IOException ex) {
              ex.printStackTrace();
          }
      }
  
      ///////////////////////////////////////////////////Set up tread job for incoming from server
      public class IncomingReader implements Runnable {
  
          public void run(){
  
              String message;
              String[] incoming;
  
              try {
                  while ((message = reader.readLine()) != null){
                          incoming = message.split(&quot;,&quot;);
                      
                          if (incoming[1].equals(&quot;player&quot;)) {
                              players.append(incoming[0]+&quot;\n&quot;);
                              
          
                          }else if (incoming[1].equals(&quot;me&quot;)) {
                              Avatar.setName(incoming[0]);
                              System.out.println(&quot;unique update Name: &quot;+Avatar.getName());
                              
                          }else if (incoming[1].equals(&quot;remove&quot;)) {
                              
                              System.out.println(&quot;removeID received: &quot;+incoming[0]);
                              //String text = players.getText();
                              //text=text.replaceAll(incoming[0],&quot;&quot;);
                              //players.setText(&quot;&quot;);
                              //players.setText(text);
                              //players.update(players.getGraphics());
                  }
                      
                  }   
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      
  }
  
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  
  public class BattleArenaServer {
      
          Map&lt;String,Socket&gt; phonebook1 = new HashMap&lt;&gt;();
          Map&lt;String,Socket&gt; phonebook2 = new HashMap&lt;&gt;();
          Map&lt;String,Socket&gt; phonebook3 = new HashMap&lt;&gt;();
          String userName;
          BufferedReader reader;
          PrintWriter writer;
          Socket sock;
          
          
          public static void main(String[] args){         //Running the code
                  
              BattleArenaServer BAS = new BattleArenaServer();
              BAS.go1();
          }
          
          public void go1(){
                  
                  
                  try{
                      ServerSocket serverSock = new ServerSocket(5000);       //Listen at port 5000
                      
          ///////////////////////////////////////////////////////////////make outgoing connections to all clients
                      while(true){
                          
                              Socket clientSocket = serverSock.accept();          //Waiting for client and assign it a new port
                              
                              Thread t = new Thread(new ClientHandler(clientSocket));     //Make a new thread which handles the clients at new port
                              t.start();                                                  //Start the new thread
                              
                      }
                  }catch(Exception ex){ex.printStackTrace();}
          }
  
          ////////////////////////////////////////////////////////Client handling thread job - making ingoing connections
          public class ClientHandler implements Runnable {
              
                  
                  public ClientHandler(Socket clientSocket){
                          
                  /////////////////////////////////////////////////make input stream
                          try{
                              sock = clientSocket;
                              InputStreamReader isReader = new InputStreamReader(sock.getInputStream());      //Make an ingoing reader connection with client at new port
                              reader = new BufferedReader(isReader);                                          
                          }catch(Exception ex){ex.printStackTrace();}
                  }
                  
                  public void run(){
                      
                          String message;
                          
                              try {
                                  while ((message = reader.readLine())!=null){            //Read each line of input from client
                                      
                                      String[] result1 = message.split(&quot;,&quot;);
                                      
                                      if (result1[1].trim().equals(&quot;join&quot;)) {
                                          makePhonebook(message);
                                          
                                          try {
                                              writer = new PrintWriter(sock.getOutputStream());  //Update players their name with extension
                                              writer.println(userName+&quot;,me&quot;);
                                              writer.flush();
                                          }catch(Exception ex){ex.printStackTrace();} 
                                          
                                          if (phonebook2.containsKey(userName)==false) {
                                                  tellNewUser(phonebook1);
                                                  phonebook2.put(userName,sock);}
                                          
                                          for (String entry:phonebook1.keySet()) {
                                          
                                              if (entry!=userName) {
                                                  tellOldUsers(entry);
                                              }
                                          }
                                      }
                                      else if (result1[1].trim().equals(&quot;remove&quot;)) {
                                          //phonebook1.remove(result1[0].trim());
                                          for (String player:phonebook1.keySet()) {
                                              try {
                                                  PrintWriter writer = new PrintWriter(phonebook1.get(player).getOutputStream()); //pick the next connection
                                                  writer.println(result1[0]+&quot;,remove&quot;);                               //Print to next connection
                                                  writer.flush();
                                              }catch(Exception ex){ex.printStackTrace();} 
                                          }
                                      }
                                  }
                              } catch (IOException e) {e.printStackTrace();}
                  }
                  
                  public void makePhonebook(String message) {
                      
                      String[] result = message.split(&quot;,&quot;);
                      userName= result[0];
                      int i=1;
                      while(phonebook1.containsKey(userName)) {
                      String[] name = userName.split(&quot;_&quot;);
                          userName = name[0]+&quot;_(&quot;+i+&quot;)&quot;;
                          i++;
                      }
                      phonebook1.put(userName,sock);
                  }
                  
                  
                  
                  
                  public void tellNewUser(Map&lt;String,Socket&gt; pb1) {
                      
                      try {
                              phonebook3.clear();
                              phonebook3.putAll(pb1);
                              phonebook3.remove(userName);
                              
                              PrintWriter writer = new PrintWriter(sock.getOutputStream());
                              //BufferedWriter writer2 = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
                              
                              Set&lt;String&gt; keys1 = phonebook3.keySet();
                              for (String k1:keys1) {
                                  writer.println(k1+&quot;,player&quot;);
                                  
                              }
                                  writer.flush();
                          
                      }catch(Exception ex){ex.printStackTrace();} 
                  }
                                  
                  
                  public void tellOldUsers(String key) {
                              
                          try {
                                  //BufferedWriter writer3 = new BufferedWriter(new OutputStreamWriter(phonebook1.get(key).getOutputStream()));
                                  PrintWriter writer = new PrintWriter(phonebook1.get(key).getOutputStream()); //pick the next connection
                                  writer.println(userName+&quot;,player&quot;);                             //Print to next connection
                                  writer.flush();
                              
                          }catch(Exception ex){ex.printStackTrace();} 
                  }
                  
                  
                  //public void closeEverything(Socket sock,BufferedReader reader,PrintWriter writer) {
                  //      try {
                  //          if (reader != null) {
                  //              reader.close();
                  //          }
                  //          if (writer != null) {
                  //              writer.close();
                  //          }
                  //          if (sock != null) {
                  //              sock.close();
                  //          }
                  //      }catch (IOException e) {
                  //          e.printStackTrace();
                  //      }
                          
                  //}
          }
      
  }
              
  
      
  </code></pre>