title: Multi threaded chat server
body: <p>I am new to Threads. I am trying to broadcast message from the server to clients.but I cannot. It seems like the server is always listen to a new connection.But I can send messages to other clients using the clients terminal. My question is how to allow input while listening at the same time. Here is the code. </p>
  
  <p>MultiThreadChatServerSync.java</p>
  
  <pre><code>import java.io.InputStreamReader;
  import java.io.DataInputStream;
  import java.io.PrintStream;
  import java.io.IOException;
  import java.net.Socket;
  import java.net.ServerSocket;
  import java.io.BufferedReader;
  
  public class MultiThreadChatServerSync extends Thread {
  
     private static ServerSocket serverSocket = null;
     private static Socket clientSocket = null;
     private static Socket serversSocket = null;
     private static DataInputStream iss = null;
     private static PrintStream oss = null;
  
     private static final int maxClientsCount = 10;
     private static final clientThread[] threads = new clientThread[maxClientsCount];
  
     public static void main(String args[]) {
        int num = 0;
        int portNumber = 2222;
        if (args.length &lt; 1) {
           System.out.println("Usage: java MultiThreadChatServerSync&lt;portNumber&gt; "Nowusingnumber=" + portNumber);
        } else {
           portNumber = Integer.valueOf(args[0]).intValue();
        }
        try {
           serverSocket = new ServerSocket(portNumber);
        } catch (IOException e) {
           System.out.println(e);
        }
        DataInputStream iss = null;
        while (true) {
           try {
              System.out.println("5");
  
              num++;
  
              // I want to be able to input text here to the server which will be
              // send to different clients
              // But I cannot
  
              clientSocket = serverSocket.accept();
              int i = 0;
              for (i = 0; i &lt; maxClientsCount; i++) {
                 if (threads[i] == null) {
                    System.out.println("6");
                    (threads[i] = new clientThread(clientSocket, threads))
                          .start();
                    break;
                 }
              }
              if (i == maxClientsCount) {
                 PrintStream os = new PrintStream(clientSocket.getOutputStream());
                 os.println("Server too busy. Try later.");
                 os.close();
                 clientSocket.close();
              }
           } catch (IOException e) {
              System.out.println(e);
           }
        }
  
     }
  }
  
  class clientThread extends Thread {
  
     private String clientName = null;
     private DataInputStream is = null;
     private PrintStream os = null;
     private Socket clientSocket = null;
     private final clientThread[] threads;
     private int maxClientsCount;
  
     public clientThread(Socket clientSocket, clientThread[] threads) {
        this.clientSocket = clientSocket;
        this.threads = threads;
        maxClientsCount = threads.length;
     }
  
     public void run() {
        int maxClientsCount = this.maxClientsCount;
        clientThread[] threads = this.threads;
  
        try {
  
           is = new DataInputStream(clientSocket.getInputStream());
           os = new PrintStream(clientSocket.getOutputStream());
           String name;
           while (true) {
              os.println("Enter your name.");
              name = is.readLine().trim();
              if (name.indexOf('@') == -1) {
                 break;
              } else {
                 os.println("The name should not contain '@' character.");
              }
           }
  
           os.println("Welcome " + name
                 + " to our chat room.\nTo leave enter /quit in a new line.");
           synchronized (this) {
              for (int i = 0; i &lt; maxClientsCount; i++) {
                 if (threads[i] != null &amp;&amp; threads[i] == this) {
                    clientName = "@" + name;
                    break;
                 }
              }
              for (int i = 0; i &lt; maxClientsCount; i++) {
                 if (threads[i] != null &amp;&amp; threads[i] != this) {
                    threads[i].os.println("*** A new user " + name
                          + " entered the chat room !!! ***");
                 }
              }
           }
  
           while (true) {
              String line = is.readLine();
              if (line.startsWith("/quit")) {
                 break;
              }
  
              if (line.startsWith("@")) {
                 String[] words = line.split("\\s", 2);
                 if (words.length &gt; 1 &amp;&amp; words[1] != null) {
                    words[1] = words[1].trim();
                    if (!words[1].isEmpty()) {
                       synchronized (this) {
                          for (int i = 0; i &lt; maxClientsCount; i++) {
                             if (threads[i] != null &amp;&amp; threads[i] != this
                                   &amp;&amp; threads[i].clientName != null
                                   &amp;&amp; threads[i].clientName.equals(words[0])) {
                                threads[i].os.println("&lt;" + name + "&gt; "
                                      + words[1]);
  
                                this.os.println("&gt;" + name + "&gt; " + words[1]);
                                break;
                             }
                          }
                       }
                    }
                 }
              } else {
  
                 synchronized (this) {
                    for (int i = 0; i &lt; maxClientsCount; i++) {
                       if (threads[i] != null &amp;&amp; threads[i].clientName != null) {
                          threads[i].os.println("&lt;" + name + "&gt; " + line);
                       }
                    }
                 }
              }
           }
           synchronized (this) {
              for (int i = 0; i &lt; maxClientsCount; i++) {
                 if (threads[i] != null &amp;&amp; threads[i] != this
                       &amp;&amp; threads[i].clientName != null) {
                    threads[i].os.println("*** The user " + name
                          + " is leaving the chat room !!! ***");
                 }
              }
           }
           os.println("*** Bye " + name + " ***");
  
           synchronized (this) {
              for (int i = 0; i &lt; maxClientsCount; i++) {
                 if (threads[i] == this) {
                    threads[i] = null;
                 }
              }
           }
  
           is.close();
           os.close();
           clientSocket.close();
        } catch (IOException e) {
        }
     }
  }
  </code></pre>