title: Android socket programming to able to read from inputstream
body: <p>I am trying to make a messaging app using socket programming. Everything is working fine I am able to connect to the device which acts like a server with another device which acts like a client.</p>
  
  <p>This is my class for Reading and writing (Same for both Server and client):</p>
  
  <pre><code>public class MessageReader extends Thread{
      private Socket socket;
      private View view;
      private Activity activity;
      EditText message;
      Button send;
      PrintWriter out = null;
      private TextView textView;
      private String text;      
  
      public MessageReader(Socket client, View root){
          socket = client;;
           this.view = root;
           activity = (Activity)root.getContext();
      }    
  
      @Override
      public void run() {    
          try {
              out = new PrintWriter(socket.getOutputStream());
          } catch (IOException e) {
              e.printStackTrace();
          }
          String clientMsg;
          update("Now in MessageReader");
          message = (EditText) view.findViewById(R.id.yourMessage);
          send = (Button) view.findViewById(R.id.send);
          textView = (TextView) view.findViewById(R.id.message);
  
          // will write to the output stream when send button is clicked
          send.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  update("about to send message");
                  text = message.getText().toString();
                  out.println(text);
                  update(text);
               }
           });
  
          try{
              //Code for reading from the output
              BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              update("waiting for a message");
              while ((clientMsg=in.readLine())!=null){
                  update(clientMsg);
                  updateUi(clientMsg);    
              }
              in.close();
              socket.close();
              update("Connection Closed");
          }catch (IOException e){
              update(e.toString());
          }
      }
  
      public void update(final String msg){
          activity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                 Toast.makeText(activity,msg,Toast.LENGTH_LONG).show();
              }
          });
      }
  
      public void updateUi(final String message){
          activity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  textView.setText(message);
              }
          });
      }
  }
  </code></pre>
  
  <p>I get a toast when they are connected. But I am not able to read from the input stream. I don't get any errors or exceptions or any Toasts. Can someone please help me out with this?</p>