title: Android TCP server broadcast
body: <p>I'm developing an Android application which uses TCP for device connection. The problem is I'm new to socket programming. I've successfully made a server and client code. Each client can connect to the server and server can reply to the client. But I can't seem to make the server to send message to all connected client at the same time. What are the steps to make the server broadcast a message to client?
  This is the server code:</p>
  
  <pre><code>ServerSocket server = null;
  try {
      server = new ServerSocket(9092); // start listening on the port
  } catch (IOException e) {
      Log.d("btnCreate onClick", "Could not listen on port: 9092");
  }
  Socket client = null;
  while(true) {
      try {
          client = server.accept();
      } catch (IOException e) {
          Log.d("btnCreate onClick", "Accept Failed");
      }
      //start a new thread to handle this client
      Thread t = new Thread(new ClientConn(client));
      t.start();
  }
  </code></pre>
  
  <p>And the server thread:</p>
  
  <pre><code>class ClientConn implements Runnable {
      private Socket client;
  
      ClientConn(Socket client) {
          this.client = client;
      }
  
      public void run() {
          BufferedReader in = null;
          PrintWriter out = null;
          try {
              /* obtain an input stream to this client ... */
              in = new BufferedReader(new InputStreamReader(
                          client.getInputStream()));
              /* ... and an output stream to the same client */
              out = new PrintWriter(client.getOutputStream(), true);
          } catch (IOException e) {
              e.printStackTrace();
              return;
          }
  
          String msg;
          try {
              while ((msg = in.readLine()) != null) {
                  Log.d("ClientConn", "Client says: " + msg);
                  out.println(msg);
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>