title: android server client chat using socket
body: <p>I'm creating a android server client chat app using socket programming. Till now in my app the client messages will only display in the server. messages from server is not displaying. i'm attaching the java code only because i think from that u will get an idea about the xml layout components. </p>
  
  <p>server.java</p>
  
  <pre><code>package com.example.androidsocketserver;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
   public class Server extends ActionBarActivity {
      private ServerSocket serverSocket;
      private Button btn;
      private boolean willsendMsg = false;
      Handler handler;
      Thread serverThread = null;
      public static final int serverPort = 6000;
      public static String SERVERIP = "10.0.2.15";
      TextView serverStatus;
      TextView text;
       EditText msgg;
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_server);
          serverStatus = (TextView)findViewById(R.id.status);
          text = (TextView)findViewById(R.id.chatt);
         msgg = (EditText)findViewById(R.id.mesg);
         btn = (Button)findViewById(R.id.send);
              btn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              willsendMsg = true;
          }
          });
         handler = new Handler();
         this.serverThread = new Thread(new ServerThread());
         this.serverThread.start();
      }
  
  
   public class ServerThread implements Runnable{
     Socket socket = null;
      @Override
      public void run() {
          // TODO Auto-generated method stub
          try {
              serverSocket = new ServerSocket(serverPort);
              while(true){
                  socket = serverSocket.accept();
                  handler.post(new Runnable(){
                      public void run(){
                          serverStatus.setText("New Connection accepted,and the address is" + socket.getInetAddress() + ", and the port is " + socket.getLocalPort());
                      }
                  });
  
                  CommunicationThread comm = new CommunicationThread(socket);
                  new Thread(comm).start();
              }
  
          } catch (IOException e) {
              // TODO Auto-generated catch block
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      serverStatus.setText("Couldn't detect internet connection.");
                  }
              });
              e.printStackTrace();
          }
  
      }
  
  }
  
  public class CommunicationThread implements Runnable{
      private Socket client;
      BufferedReader input;
      String chat = msgg.getText().toString();
      public CommunicationThread(Socket clientt){
          this.client = clientt;
          try {
  
              this.input = new BufferedReader(new InputStreamReader(client.getInputStream()));
              if(willsendMsg){
                  willsendMsg=false;
                  OutputStream outputSteam = client.getOutputStream();
                  PrintWriter pw = new PrintWriter(outputSteam, true);
                  pw.println(chat);
              }
  
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      @Override
      public void run() {
          // TODO Auto-generated method stub
          while(!Thread.currentThread().isInterrupted()){
              try {
                  final String read = input.readLine();
                  handler.post(new Runnable(){
                      public void run(){
                          text.setText(text.getText().toString()+"Client :"+read+"\n");
                      }
                  });
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
  }
  
  protected void onStop() {
      super.onStop();
      try {
          serverSocket.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>the Client.java</p>
  
  <pre><code>package com.example.androidclientsocket;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
   import android.support.v7.app.ActionBarActivity;
   import android.os.Bundle;
   import android.os.Handler;
  import android.view.Menu;
  import android.view.MenuItem;
   import android.view.View;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class Clientsocket extends ActionBarActivity {
    private Socket socket;
    private static final int serverIPPort = 5000;
    private static String server_IP = "10.0.2.2";
    Handler handlers;
    TextView inb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_clientsocket);
      inb = (TextView)findViewById(R.id.inbox);
      handlers = new Handler();
      new Thread(new ClientThread()).start();
      }
     public class ClientThread implements Runnable{
  
        @Override
            public void run() {
      // TODO Auto-generated method stub
       try {
      InetAddress serverAdd = InetAddress.getByName(server_IP);
      socket = new Socket(serverAdd,serverIPPort);
      BufferedReader inputt = new BufferedReader(new      InputStreamReader(socket.getInputStream()));
        final String box = inputt.readLine();
        handlers.post(new Runnable(){
          public void run(){
              inb.setText(inb.getText().toString()+"Server :"+box+"\n");
          }
          });
        } catch (UnknownHostException e) {
         // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
       }  
  
     }
  
   }
  
   public void Posting(View v){
    EditText et = (EditText)findViewById(R.id.msg);
      String message = et.getText().toString();
      try {
      PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
      out.println(message);
      et.setText("");
      } catch (UnknownHostException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      } catch (Exception e) {
      e.printStackTrace();
      }
   }
  
      protected void onStop() {
      super.onStop();
      try {
          socket.close();
      } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  </code></pre>
  
  <p>the logcat didn't shows any exceptions or errors on button click. I have already added internet and access network permissions in manifest files. please help me to find the problem which causes <strong>the server message from not sending to client and displaying there.</strong></p>