title: Send message from server to all clients using socket in Android
body: <p>I am successfully sent message from client to server and my code sent message to last connected client, not to all connected clients. I need to implement send message to all connected clients. my code is bellow.</p>
  
  <p>client </p>
  
  <hr>
  
  <pre><code> public class Client extends Activity {
  
  private Socket socket;
  Button connectBtn;
  EditText ip;
  private static final int SERVERPORT = 8080;
  private String SERVER_IP;
  private Socket clientSocket;
  String response = "";
  private TextView text;
  Handler updateConversationHandler;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ip = (EditText)findViewById(R.id.EditTextIP);
      text = (TextView)findViewById(R.id.msg);
      updateConversationHandler = new Handler();
      connectBtn = (Button)findViewById(R.id.ipButton);
      connectBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              SERVER_IP = ip.getText().toString();
              Toast.makeText(Client.this,"ip set",Toast.LENGTH_SHORT).show();
              new Thread(new ClientThread()).start();
          }
      });
  }
  
  public void onClick(View view) {
      try {
          EditText et = (EditText) findViewById(R.id.EditText01);
          String str = et.getText().toString();
          PrintWriter out = new PrintWriter(new BufferedWriter(
                  new OutputStreamWriter(socket.getOutputStream())),
                  true);
          out.println(str);
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  class ClientThread implements Runnable {
  
      @Override
      public void run() {
          try {
              InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
              socket = new Socket(serverAddr, SERVERPORT);
              CommunicationThread commThread = new CommunicationThread(socket);
              new Thread(commThread).start();
          } catch (UnknownHostException e1) {
              e1.printStackTrace();
          } catch (IOException e1) {
              e1.printStackTrace();
          }
  
  
      }
  }
  
  class CommunicationThread implements Runnable {
      // private Socket clientSocket;
      private BufferedReader input;
      public CommunicationThread(Socket aClientSocket)
      {
          clientSocket = aClientSocket;
          try {
              this.input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
      public void run()
      {
          while (!Thread.currentThread().isInterrupted())
          {
              try
              {
                  String read = input.readLine();
                  Log.d("msg","readString"+read);
                  updateConversationHandler.post(new updateUIThread(read));
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  
  class updateUIThread implements Runnable {
      private String msg;
      public updateUIThread(String str) {
          this.msg = str;
      }
  
      @Override
      public void run() {
          //if(msg!=null)
          text.setText("Server Says: "+ msg + "\n");
      }
  }
  
  }
  </code></pre>
  
  <p>Server</p>
  
  <hr>
  
  <pre><code>public class MainActivity extends Activity {
  
  private ServerSocket serverSocket;
  Handler updateConversationHandler;
  Thread serverThread = null;
  private TextView text,iptext;
  public static final int SERVERPORT = 8080;
  String message;
  Button btn;
  private Socket socket;
  Socket  clientSocket;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      text = (TextView) findViewById(R.id.text2);
      iptext = (TextView) findViewById(R.id.ip);
      //btn = (Button)findViewById(R.id.send);
      iptext.setText(getIpAddress());
      updateConversationHandler = new Handler();
      this.serverThread = new Thread(new ServerThread());
      this.serverThread.start();
  
  
  }
  
  public void onClick(View view) {
      try {
          if(clientSocket==null)
              Toast.makeText(MainActivity.this, "null socket", Toast.LENGTH_SHORT).show();
          else
          {
              Toast.makeText(MainActivity.this, "Msg send to socket", Toast.LENGTH_SHORT).show();
              PrintWriter out = new PrintWriter(new BufferedWriter(
                      new OutputStreamWriter(clientSocket.getOutputStream())),
                      true);
              out.println("viky");
          }
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  @Override
  protected void onStop() {
      super.onStop();
      try {
          serverSocket.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  class ServerThread implements Runnable {
  
      public void run() {
          Socket socket = null;
          try {
              serverSocket = new ServerSocket(SERVERPORT);
          } catch (IOException e) {
              e.printStackTrace();
          }
          while (!Thread.currentThread().isInterrupted()) {
              try {
                  Log.e("ServerThread","CommunicationThread started");
                  socket = serverSocket.accept();
                  CommunicationThread commThread = new CommunicationThread(socket);
                  new Thread(commThread).start();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  
  public String getIpAddress() {
      String ip = "";
      try {
          Enumeration&lt;NetworkInterface&gt; enumNetworkInterfaces = NetworkInterface
                  .getNetworkInterfaces();
          while (enumNetworkInterfaces.hasMoreElements()) {
              NetworkInterface networkInterface = enumNetworkInterfaces
                      .nextElement();
              Enumeration&lt;InetAddress&gt; enumInetAddress = networkInterface
                      .getInetAddresses();
              while (enumInetAddress.hasMoreElements()) {
                  InetAddress inetAddress = enumInetAddress
                          .nextElement();
  
                  if (inetAddress.isSiteLocalAddress()) {
                      ip += "Server running at : "
                              + inetAddress.getHostAddress();
                  }
              }
          }
  
      } catch (SocketException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          ip += "Something Wrong! " + e.toString() + "\n";
      }
      return ip;
  }
  
  class CommunicationThread implements Runnable {
  
      // private Socket clientSocket;
      private BufferedReader input;
      public CommunicationThread(Socket aClientSocket) {
          clientSocket = aClientSocket;
          try {
              this.input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void run() {
          while (!Thread.currentThread().isInterrupted()) {
  
              try {
                  String read = input.readLine();
                  if(read!=null &amp;&amp; !read.equals(""))
                  updateConversationHandler.post(new updateUIThread(read));
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  
  class updateUIThread implements Runnable {
      private String msg;
  
      public updateUIThread(String str) {
          this.msg = str;
      }
  
      @Override
      public void run() {
          text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
      }
  }
  }
  </code></pre>
  
  <p>Do I have any mistakes in my code or something I missed?</p>