title: Java - Send Message to all Clients
body: <p>I'm creating a chat server using Socket Servers and Thread. I have 3 Classes in my Server ChatServer.java, ThreadedServer.java, and Main.java. My Client has 2 ChatClient.java, Main.java. I need to make it so when a message is sent in from a client to server, the server sends out the message to all Clients.</p>
  
  <p>ChatServer.java</p>
  
  <pre><code>package server;
  
  import java.io.*;
  import java.net.*;
  
  public class ChatServer {
      protected Socket s;
      protected Socket ss;
      public ChatServer() {
          try {
               ServerSocket ss=new ServerSocket(6969);
               Runtime.getRuntime().addShutdownHook(new Thread() {
                   public void main(String []args) throws IOException {
                      ss.close();
                  }
              });
              while (true) {
                  Socket s =ss.accept();
                  new ThreadedServer(s).start();
              }
          }catch(Exception e) {
              System.out.println(e);
          }
          new ThreadedServer(s).start();
        }
  }
  </code></pre>
  
  <p>ThreadedServer.java</p>
  
  <pre><code>package server;
  
  import java.io.*;
  import java.net.*;
  
  public class ThreadedServer extends Thread{
      protected Socket socket;
  
      public ThreadedServer(Socket clientSocket) {
          this.socket = clientSocket;
      }
  
      public void run() {
          try {
              DataInputStream dis = new DataInputStream(socket.getInputStream());
              DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
              while (true) {
                  String str=(String)dis.readUTF();
                  String remote = socket.getInetAddress().toString();
                  String newmes = remote + ": " + str;
                  dos.writeUTF(newmes);
                  System.out.println(newmes);
                  if (str.toUpperCase() == "QUIT") {
                      socket.close();
                      break;
                  }else if (str.toUpperCase() == "EXIT") {
                      socket.close();
                      break;
                  }
              }
          }catch(Exception e) {
              System.out.println(e);
          }
      }
  }
  </code></pre>
  
  <p>Main.java (Server)
      package server;</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
          ChatServer chat = new ChatServer();
      }
  }
  </code></pre>
  
  <p>Here are the Client.java's</p>
  
  <p>ChatClient.java
      package client;</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class ChatClient {
  
      public ChatClient() {
          try {
              Socket s = new Socket("10.4.27.29",6969);
              Runtime.getRuntime().addShutdownHook(new Thread() {
                  public void main(String []args) throws IOException {
                      s.close();
                  }
              });
              DataOutputStream dos = new DataOutputStream(s.getOutputStream());
              DataInputStream dis = new DataInputStream(s.getInputStream());
              while (true) {
                  String message = System.console().readLine();
                  dos.writeUTF(message);
                  System.out.println(dis.readUTF());
              }
          }catch(Exception e) {
               System.out.println(e);
          }
      }
  }
  </code></pre>
  
  <p>Main.java (Client)</p>
  
  <pre><code>package client;
  
  public class Main {
      public static void main(String[] args) {
          ChatClient chat = new ChatClient();
  
      }
  }
  </code></pre>
  
  <p>Please any help would be amazing.</p>
  
  <p>Also any ideas to make this nicer would be appreciated. Thanks!!</p>