title: Server client chat room in Java
body: <p>I was making a chat room  project where the server accepts many clients, and whatever the client writes reaches the other clients and so on.
  Unfortunately the server accepts maximum 2 clients and after one client writes an input it gives errors.</p>
  
  <pre><code>public class Server2 {
  
      private static ArrayList&lt;Socket&gt; clients;
      private ServerSocket server;
      DataOutputStream os;
      DataInputStream in;
      Socket s;
  
      public Server2() throws IOException {
          server = new ServerSocket(5000);
          clients = new ArrayList&lt;Socket&gt;();
          System.out.println("Waiting for connections...");
          runOutput();
      }
  
      public void addClient() throws IOException {
          s = server.accept();
          clients.add(s);
          System.out.println("A new Client has joined");
      }
  
      public void runOutput() {
  
          Thread n = new Thread(new Runnable() {
              public void run() {
                  try {
                      addClient();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          });
          n.start(); 
  
          Thread input = new Thread(new Runnable() {
              public void run() {
                  try {
                      addClient();
                      in = new DataInputStream(s.getInputStream());
                      os = new DataOutputStream(s.getOutputStream());
                      String st = in.readLine();
                      System.out.println(st);
                      os.writeBytes(st);
                      for(int i = 0; i &lt; clients.size(); i++)
                      {
                          DataOutputStream oo = new DataOutputStream(clients.get(i).getOutputStream());
                          oo.writeBytes(st);
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          });
          input.start();
      }
  
      public static void main(String[] args) throws IOException {
          Server2 server = new Server2();
      }
  }
  </code></pre>
  
  <p>and the client class : </p>
  
  <pre><code>public class Client2 {
      final public static String host = "localhost";
      final public static int port = 5000;
      Socket socket;
      DataInputStream in;
      DataOutputStream ou;
      Scanner chat;
      boolean run;
      String name;
  
      public Client2(String n) throws IOException {
          name = n ;
          socket = new Socket(host , port);
          System.out.println("Connection Successful");
          run = true;
          runOutput();
      }
  
      public void runOutput() {
          Thread input = new Thread(new Runnable() {
              public void run() {
                  while (run) {
                      try {
                          in = new DataInputStream(socket.getInputStream());
                          String s = in.readLine();
                          System.out.println(s);
                          if(chat.nextLine().compareTo("QUIT") == 0)
                              run = false;
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          });
          input.start();
  
          Thread t = new Thread(new Runnable() {
              public void run() {
                  while (run) {
                      try {
                          ou = new DataOutputStream(socket.getOutputStream());
                          chat = new Scanner(System.in);
                          ou.writeBytes(name + " says :" + chat.nextLine() + "\n");
  
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          });
  
          t.start(); 
      } 
  
      public static void main(String[] args) throws IOException {
          Client2 client = new Client2("Ahmed");
      }   
  }
  </code></pre>