title: Socket Client stuck on 'waiting for message from server.'
body: <p>Backstory: 
  I created a Barcode Scanning app that can connect with my works server. The Barcode app functions perfectly, however, I have no experience with socket connections to servers. So in order to not mess up any code in the Barcode app I copied a tutorial for a socket connection and edited it. Once I can get the test code to complete a connection and send data back and forth, I will implement it into the Barcode app. </p>
  
  <p>The Problem:
  When I click my button that says "Connect to server" I get this:</p>
  
  <pre><code>03-23 11:55:52.174 6438-6459/com.example.vipin.client I/Client: Waiting for message from server...
  </code></pre>
  
  <p>Pasted below is the code. I have removed the IP address and Port for security reasons.</p>
  
  <pre><code>package com.example.vipin.client;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.TextView;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  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 java.text.SimpleDateFormat;
  import java.util.Date;
  
  public class Client extends AppCompatActivity implements View.OnClickListener {
  
  public static final String TAG = Client.class.getSimpleName();
  
  public static final int SERVERPORT = 0000;
  
  public static final String SERVER_IP = "000.000.0.0";
  ClientThread clientThread;
  Thread thread;
  TextView messageTv;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      messageTv = (TextView) findViewById(R.id.messageTv);
  }
  
  public void updateMessage(final String message) {
      runOnUiThread(new Runnable() {
          @Override
          public void run() {
              messageTv.append(message + "\n");
          }
      });
  }
  
  @Override
  public void onClick(View view) {
  
      if (view.getId() == R.id.connect_server) {
          messageTv.setText("");
          clientThread = new ClientThread();
          thread = new Thread(clientThread);
          thread.start();
          return;
      }
  
      if (view.getId() == R.id.send_data) {
          clientThread.sendMessage("Hello from Client");
          Log.i(TAG, "Sending message to server...");
      }
  }
  
  class ClientThread implements Runnable {
  
      private Socket socket;
      private BufferedReader input;
  
      @Override
      public void run() {
  
          try {
              InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
              socket = new Socket(serverAddr, SERVERPORT);
  
              while (!Thread.currentThread().isInterrupted()) {
  
                  Log.i(TAG, "Waiting for message from server...");
  
                  this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  String message = input.readLine();
                  Log.i(TAG, "Message received from the server : " + message);
  
                  if (null == message || "Disconnect".contentEquals(message)) {
                      Thread.interrupted();
                      message = "Server Disconnected.";
                      updateMessage(getTime() + " | Server : " + message);
                      break;
                  }
  
                  updateMessage(getTime() + " | Server : " + message);
  
              }
  
          } catch (UnknownHostException e1) {
              e1.printStackTrace();
          } catch (IOException e1) {
              e1.printStackTrace();
          }
  
      }
  
      void sendMessage(String message) {
          try {
              if (null != socket) {
                  PrintWriter out = new PrintWriter(new BufferedWriter(
                          new OutputStreamWriter(socket.getOutputStream())),
                          true);
                  out.println(message);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
  
  
      }
  
  }
  
  String getTime() {
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
      return sdf.format(new Date());
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      if (null != clientThread) {
          clientThread.sendMessage("Disconnect");
          clientThread = null;
      }
  }
  }
  </code></pre>
  
  <p>Log for the button click event is here <a href="https://pastebin.com/6wbvLj6w" rel="nofollow noreferrer">https://pastebin.com/6wbvLj6w</a></p>