title: How do i send the message to multiple clients in java Client-Server Program
body: <p>Multiple Clients say(A, B, C, D etc) make connection to one server through same socket. They all send messages to server as and when required. Client messages are sent only to server(and not to other clients). But whenever server sends a message it should be delivered to all the clients. Please help me out jam only able to get server message on only 1 client</p>
  
  <p>//MultithreadedServer.java</p>
  
  <pre><code>package server;
  import java.io.*;
  
  import java.net.*;
  import java.util.ArrayList;
  import java.util.Vector;
  public class MultithreadedServer
  {
  
  
  
  Vector&lt;ClientHandler&gt; clients = new Vector&lt;ClientHandler&gt;();
      Vector&lt;String&gt; users = new Vector&lt;String&gt;();
      private static ServerSocket servSocket;
      private static final int PORT = 1247;
  
  public MultithreadedServer() throws IOException{
      servSocket = new ServerSocket(PORT);
    while(true) {
  
                  Socket client = servSocket.accept();
                  System.out.println("\nNew client accepted.\n");
  
  
  
  
      ClientHandler handler;
          handler = new ClientHandler(client);
          clients.add(handler);
  
  }
  }
  public static void main(String[] args) throws IOException
  {
      MultithreadedServer ms = new MultithreadedServer();
  
  }
  class ClientHandler extends Thread
  {
  
  private Socket client;
  private BufferedReader in;
  private PrintWriter out;
  String name,message,response;
  
  public ClientHandler(Socket socket)
  {
      //Set up reference to associated socket...
      client = socket;
      try
      {
          in = new BufferedReader(
                  new InputStreamReader(
                          client.getInputStream()));
          out = new PrintWriter(
                      client.getOutputStream(),true);
      }
      catch(IOException e)
      {
          e.printStackTrace();
      }
      start();
  }
  public void sendMessage(String  msg)  {
          System.out.println("is it even coming here?");
      out.println("Server:" + msg);
  }
  public void boradcast(String message)  {
      // send message to all connected users
      for (ClientHandler c : clients) {
  
            c.out.println("Server: hello");
  }
  }
  public String getUserName() {  
          return name; 
      }
   public void run()
    {
      try
      {
          String received;
          do
          {   System.out.println("Enter Your Message: ");
              String msg = in.readLine();
              out.println(msg);
              boradcast("testing");
              received = in.readLine();
              out.println("ECHO: " + received);
          //Repeat above until 'QUIT' sent by client...
  
          }while (!received.equals("QUIT"));
      }
      catch(IOException e)
      {
          e.printStackTrace();
      }
      finally
      {
          try
          {
              if (client!=null)
              {
                  System.out.println(
                              "Closing down connection...");
                  client.close();
                          }
          }catch(IOException e)
          {
              e.printStackTrace();
          }
        }
     }
  }}
  //ClientProgram
  
  import java.io.*;
  import java.net.*; 
  public class Client
   {
  private static InetAddress host;
  private static final int PORT = 1247;
  private static Socket link;
  private static BufferedReader in;
  private static PrintWriter out;
  private static BufferedReader keyboard; 
  public static void main(String[] args)  throws IOException
  {
      try
    {
          host = InetAddress.getLocalHost();
          link = new Socket(host, PORT);
          in = new BufferedReader(
                  new InputStreamReader(
                              link.getInputStream()));
          out = new PrintWriter(
                              link.getOutputStream(),true);
          keyboard = new BufferedReader(
                          new InputStreamReader(System.in));
          String message, response;
          do
          {
              System.out.print(
                    "Enter message ('QUIT' to exit): ");
              message = keyboard.readLine();
             //Send message to server on
              //the socket's output stream...
              out.println(message);
              //Accept response frm server on
              //the socket's input stream...
              response = in.readLine();
              //Display server's response to user...
              System.out.println(response);
          }while (!message.equals("QUIT"));
      }
      catch(UnknownHostException uhEx)
      {
          System.out.println(
                      "\nHost ID not found!\n");
      }
      catch(IOException ioEx)
      {
          ioEx.printStackTrace();
      }
      finally
      {
          try
          {
              if (link!=null)
              {
                  System.out.println(
                          "Closing down connection...");
                  link.close();
              }
          }
          catch(IOException ioEx)
          {
              ioEx.printStackTrace();
          }
         }
        }
     }
  </code></pre>