title: java multi client socket server echoing all clients
body: <p>I was working on multi-client sockets and its working just fine, however it came to my mind on how to make the communication public by making the entered string being streamed to all clients.</p>
  
  <p>e.g if there are lets say 3 clients A,B and C and client A sends "foo" to server, I want the server to stream "foo" to clients B and C as well. </p>
  
  <p>The Server Module :</p>
  
  <pre><code>package multiclient;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server {
  
  public static void main(String args[]) {
  
      Socket s = null;
      ServerSocket ss2 = null;
      System.out.println("Server Listening......");
      try {
          ss2 = new ServerSocket(4445); // can also use static final PORT_NUM , when defined
  
      } catch (IOException e) {
          e.printStackTrace();
          System.out.println("Server error");
  
      }
  
      while (true) {
          try {
              s = ss2.accept();
              System.out.println("connection Established");
              ServerThread st = new ServerThread(s);
              st.start();
  
          } catch (Exception e) {
              e.printStackTrace();
              System.out.println("Connection Error");
  
          }
      }
  
    }
  
              }
  
  class ServerThread extends Thread {
  
  String line = null;
  BufferedReader is = null;
  PrintWriter os = null;
  Socket s = null;
  
  public ServerThread(Socket s) {
      this.s = s;
  }
  
  public void run() {
      try {
          is = new BufferedReader(new InputStreamReader(s.getInputStream()));
          os = new PrintWriter(s.getOutputStream());
  
      } catch (IOException e) {
          System.out.println("IO error in server thread");
      }
  
      try {
          line = is.readLine();
          while (line.compareTo("QUIT") != 0) {
  
              os.println(line);
              os.flush();
              System.out.println("Response to Client  :  " + line);
              line = is.readLine();
          }
      } catch (IOException e) {
  
          line = this.getName(); //reused String line for getting thread name
          System.out.println("IO Error/ Client " + line + " terminated abruptly");
      } catch (NullPointerException e) {
          line = this.getName(); //reused String line for getting thread name
          System.out.println("Client " + line + " Closed");
      } finally {
          try {
              System.out.println("Connection Closing..");
              if (is != null) {
                  is.close();
                  System.out.println(" Socket Input Stream Closed");
              }
  
              if (os != null) {
                  os.close();
                  System.out.println("Socket Out Closed");
              }
              if (s != null) {
                  s.close();
                  System.out.println("Socket Closed");
              }
  
          } catch (IOException ie) {
              System.out.println("Socket Close Error");
          }
      }//end finally
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The Client Module:</p>
  
  <pre><code>package multiclient;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  
  public class Client {
  
  public static void main(String args[]) throws IOException{
  
  
  InetAddress address=InetAddress.getLocalHost();
  Socket s1=null;
  String line=null;
  BufferedReader br=null;
  BufferedReader is=null;
  PrintWriter os=null;
  
  try {
      s1=new Socket(address, 4445); // You can use static final constant PORT_NUM
      br= new BufferedReader(new InputStreamReader(System.in));
      is=new BufferedReader(new InputStreamReader(s1.getInputStream()));
      os= new PrintWriter(s1.getOutputStream());
  }
  catch (IOException e){
      e.printStackTrace();
      System.err.print("IO Exception");
  }
  
  System.out.println("Client Address : "+address);
  System.out.println("Enter Data to echo Server ( Enter QUIT to end):");
  
  String response=null;
  try{
      line=br.readLine(); 
      while(line.compareTo("QUIT")!=0){
              os.println(line);
              os.flush();
              response=is.readLine();
              System.out.println("Server Response : "+response);
              line=br.readLine();
  
          }
  
  
  
  }
  catch(IOException e){
      e.printStackTrace();
  System.out.println("Socket read Error");
  }
  finally{
  
      is.close();os.close();br.close();s1.close();
              System.out.println("Connection Closed");
  
  }
  
          }
            }
  </code></pre>