title: How can I get my chat to accept more than one client?
body: <p>I am trying to create a multi-client to server chat. This works fine while chatting one-to-one, but when I try to run another client and connect it, it does not go through/connects. Am I missing something on the threads? I only added the Server and Client classes without the imports, there is the main class that asks the user if it wants to be a server or client(join) and asks for the port...</p>
  <pre><code>    public Server(int port, String name) {
          frame = new JFrame(&quot;Messenger&quot;);
          frame.setSize(600, 400);
          hostport = port; 
          userText = new JTextField(); //Input text field
          userText.setEditable(false); //Can't write until connected with someone
          userText.addActionListener(
                  new ActionListener() {
                      public void actionPerformed(ActionEvent event) {
                          sendMessage(event.getActionCommand(), name);
                          userText.setText(&quot;&quot;);
                      }
                      
                  }
              );
          JButton jbutton = new JButton(&quot;Browse File&quot;);
          JPanel messagePanel = new JPanel(new BorderLayout());
          jbutton.addActionListener(
                  new ActionListener() {
                      public void actionPerformed(ActionEvent event) {
                          JFileChooser file = new JFileChooser();
                          file.setCurrentDirectory(new File(System.getProperty(&quot;user.home&quot;)));
                          FileNameExtensionFilter filter = new FileNameExtensionFilter(&quot;*.Images&quot;, &quot;jpg&quot;,&quot;gif&quot;,&quot;png&quot;);
                          file.addChoosableFileFilter(filter);
                          int result = file.showSaveDialog(null);
                           if(result == JFileChooser.APPROVE_OPTION){
                                File selectedFile = file.getSelectedFile();
                                String path = selectedFile.getAbsolutePath();
                                userText.setText(path);
                            }
                            else if(result == JFileChooser.CANCEL_OPTION){
                                System.out.println(&quot;No File Select&quot;);
                            }
                          }
                  }
            );
                  
                  messagePanel.add(userText, BorderLayout.CENTER);
                  messagePanel.add(jbutton, BorderLayout.EAST);
                  chatWindow = new JTextArea();
                  add(new JScrollPane(chatWindow));
                  chatWindow.setSize(400, 300);
                  frame.add(chatWindow, BorderLayout.CENTER);
                  frame.add(messagePanel, BorderLayout.SOUTH);
                  frame.setVisible(true);
      
                  t = new Thread(new Runnable() {
                       @Override
                          public void run(){
                              startRunning(name);
                          }
                      });
                   
                  }
      
      
      //set up and run de server 
      public void startRunning(String servName) {
          try {
              server = new ServerSocket(hostport);
              while(true) {
                  try {
                      waitForConnection(servName);
                      setupStreams();
                      whileChatting();    
                  }catch(EOFException eofException) {
                      showMessage(&quot;\n Connection Ended&quot;);
                  }finally {
                      close();
                  }
              }
              
          }catch(IOException ioException) {
              ioException.printStackTrace(); //prints error
          }
      }
      //wait for connection
      private void waitForConnection(String servName) throws IOException{
          showMessage(&quot;Waiting for someone to connect&quot;);
          socket = server.accept();
          showMessage(&quot;\nNow connected to: &quot; + socket.getInetAddress() + servName);
      }
      //get stream to send and receive data 
      private void setupStreams() throws IOException{
          output = new ObjectOutputStream(socket.getOutputStream());
          output.flush();
          input = new ObjectInputStream(socket.getInputStream());
          showMessage(&quot;\n Streams Are now Set up \n&quot;);
      }
      
      //during the chat conversation
      private void whileChatting() throws IOException{
          String message = &quot;You are now connected &quot;;
          showMessage(message);
          ableToType(true);
          do {
              try {
                  message = (String) input.readObject();
                  showMessage(&quot;\n&quot; + message);
              }catch(ClassNotFoundException classNotFoundException) {
                  showMessage(&quot;\n Error on input \n&quot;);
              }
              
          }while(!message.equals(&quot;CLIENT - END&quot;));
      }
      
      //Close streams and sockets in java after done chatting
      private void close() {
          showMessage(&quot;\n Closing connections... \n&quot;);
          ableToType(false);
          try {
              output.close(); //stream close
              input.close(); //stream close 
              socket.close(); //connection close
              
          }catch(IOException ioException) {
              ioException.printStackTrace(); //prints error
          }
      }
      
      //sends message to the client 
      private void sendMessage(String message, String name) {
          try {
              output.writeObject(name + &quot;: &quot; + message);
              output.flush(); //cleans input stream, flush all the extra bites 
              showMessage(&quot;\n &quot; + name + &quot;: &quot; + message);
          }catch(IOException ioException) {
              chatWindow.append(&quot;\n Error &quot;);
          }
      }
      
      //updates chat window (displays messages)
      private void showMessage(final String text) {
          SwingUtilities.invokeLater( //allows thread that updates parts of the GUI
                  new Runnable() {
                      public void run() {
                          chatWindow.append(text);
                      }
                  }
              );
          
      }
      
      //lets the user type text into their box 
      private void ableToType(final boolean tof) {
          SwingUtilities.invokeLater( //allows thread that updates parts of the GUI
                  new Runnable() {
                      public void run() {
                          userText.setEditable(tof);
                      }
                  }
              );
      }
      
  </code></pre>
  <pre><code>    public Client(String IPAdress, int port, String name){
          frame = new JFrame(&quot;Client&quot;);
          frame.setSize(600, 400);
          
          serverIP = IPAdress;
          HostPort = port;
          userText = new JTextField(); //Input text field
          userText.setEditable(false); //Can't write until connected with someone
          userText.addActionListener(
                  new ActionListener() {
                      public void actionPerformed(ActionEvent event) {
                          sendData(event.getActionCommand(), name);
                          userText.setText(&quot;&quot;);
                      }
                  }
              );
          JButton jbutton = new JButton(&quot;Browse File&quot;);
          JPanel messagePanel = new JPanel(new BorderLayout());
          jbutton.addActionListener(
                  new ActionListener() {
                      public void actionPerformed(ActionEvent event) {
                          JFileChooser file = new JFileChooser();
                          file.setCurrentDirectory(new File(System.getProperty(&quot;user.home&quot;)));
                          FileNameExtensionFilter filter = new FileNameExtensionFilter(&quot;*.Images&quot;, &quot;jpg&quot;,&quot;gif&quot;,&quot;png&quot;);
                          file.addChoosableFileFilter(filter);
                          int result = file.showSaveDialog(null);
                           if(result == JFileChooser.APPROVE_OPTION){
                                File selectedFile = file.getSelectedFile();
                                String path = selectedFile.getAbsolutePath();
                                userText.setText(path);
                            }
                            else if(result == JFileChooser.CANCEL_OPTION){
                                System.out.println(&quot;No File Select&quot;);
                            }
                          }
                  }
            );
          
          //add JTextField to CENTER and button to EAST
          messagePanel.add(userText, BorderLayout.CENTER);
          messagePanel.add(jbutton, BorderLayout.EAST);
          chatWindow = new JTextArea();
          add(new JScrollPane(chatWindow));
          chatWindow.setSize(400, 300);
          frame.add(chatWindow, BorderLayout.CENTER);
          frame.add(messagePanel, BorderLayout.SOUTH);
          frame.setVisible(true);
          
          t = new Thread(new Runnable() {
              @Override
              public void run(){
                  startRunnable();
              }
          });
       
      }
  
      //connect to server 
      public void startRunnable() {
          try {
              while(true) {
          try {
              connectToServer();
              setupStreams();
              whileChatting();
  
          }catch(EOFException eofException) {
              showMessage(&quot;\n Connection Ended&quot;);
          }finally {
              close();
          }
              }
          }catch(IOException ioException) {
              ioException.printStackTrace(); //prints error
          }
      }
   //connection to server 
      private void connectToServer() throws IOException{
          showMessage(&quot;\n Attempting connection&quot;);
          connection = new Socket(InetAddress.getByName(serverIP),HostPort);
          showMessage(&quot;\nNow connected to &quot; + connection.getInetAddress().getHostName());
      }
      
      
      //get stream to send and receive data 
          private void setupStreams() throws IOException{
              try {
              output = new ObjectOutputStream(connection.getOutputStream());
              output.flush();
              input = new ObjectInputStream(connection.getInputStream());
              showMessage(&quot;llegue?&quot;);
              showMessage(&quot;\n Streams Are now Set up \n&quot;);
              }catch(IOException ioException) {
                  chatWindow.append(&quot;\n Error seting up stream&quot;);
              }
          }
          
          //during the chat conversation
          private void whileChatting() throws IOException{
              ableToType(true);
              do {
                  try {
                      message = (String) input.readObject();
                      showMessage(&quot;\n&quot; + message);
                  }catch(ClassNotFoundException classNotFoundException) {
                      showMessage(&quot;\n Error on input \n&quot;);
                  }
                  
              }while(!message.equals(&quot;SERVER - END&quot;));
          }
  
          //Close streams and sockets in java after done chatting
          private void close() {
              showMessage(&quot;\n Closing connections... \n&quot;);
              ableToType(false);
              try {
                  output.close(); //stream close
                  input.close(); //stream close 
                  connection.close(); //connection close
                  
                  
              }catch(IOException ioException) {
                  ioException.printStackTrace(); //prints error
              }
          }
          
          //sends message to the client 
          private void sendData(String message, String name) {
              try {
                  output.writeObject(name + &quot;: &quot;+message);
                  output.flush(); //cleans input stream, flush all the extra bites 
                  showMessage(&quot;\n&quot; + name + &quot;: &quot;+ message);
              }catch(IOException ioException) {
                  chatWindow.append(&quot;\n Error &quot;);
              }
          }
          
          //updates chat window (displays messages)
          private void showMessage(final String M) {
              SwingUtilities.invokeLater( //allows thread that updates parts of the GUI
                      new Runnable() {
                          public void run() {
                              chatWindow.append(M);
                          }
                      }
                  );
              
          }
          
          //lets the user type text into their box 
          private void ableToType(final boolean tof) {
              SwingUtilities.invokeLater( //allows thread that updates parts of the GUI
                      new Runnable() {
                          public void run() {
                              userText.setEditable(tof);
                          }
                      }
                  );
          }
          
          
  }
  
  
  
  </code></pre>