title: How to avoid Buffered Reader to hang Android App? Java/Python
body: <p>I'm currently developing an Android App which needs to comunicate via sockets. The server socket is on Python. I am able to send messages and read them on python server, but when I try to send a message from server to App the App hangs and stop working (I pretty sure on the <code>bufferedread.realine()</code>). 
  The method I'm using is the <code>sendMessage</code> which returns a <code>String</code> that reads from the <code>InputStream</code> but it actually hangs when trying to do that. How can I fix this?
  Thanks BTW</p>
  
  <p>PYTHON SERVER CODE</p>
  
  <pre><code>import socket
  from threading import Thread
  import threading
  import os
  
  global init,userCount
  puerto = 999
  init = False
  userCount = 0
  def setupServer():
      global server,init
      server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      try:
          print("PUERTO: "+str(puerto))
          server.bind(('', puerto))
          init = True
          server.listen(5)
      except:
          print("Failed to create socket")
  
  def listen():
      global server,init,userCount
      if(init == True):
          print("- Server Initialized -")
      while True:
          conn, addr = server.accept()
          userCount+=1
          print("**User Connected**" + " User:"+str(userCount))
          b= Thread(target=handleClient, args=(conn,addr))
          b.daemon = True
          b.start()
          b.join()
          if(userCount == 0):
              break
          server.close()
      print("- Server Closed -")
  def handleClient(conn,addr):
      global userCount
      while True:
          data = conn.recv(1024)
          data = data.decode("utf-8")
          try:
              if(data != ""):
                  if(data == "exit\n"):
                      break
                  message = "Received\n"
                 conn.send(message)
                 print(data)
  
          except:
              pass
      userCount-=1
  setupServer()
  listen()
  </code></pre>
  
  <p>CLASS OF SOCKET AT ANDROID APP</p>
  
  <pre><code>public class SocketClient implements Runnable {
      private Socket socket;
      public PrintWriter mBufferOut;
      private BufferedReader mBufferIn;
  
      public SocketClient(String pAdress, int pPort) {
  
          try {
              socket = new Socket(pAdress, pPort);
  
              try {
                  mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                  mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              }catch (Exception e) {
                  Log.e("TCP", "S: Error", e);
              }
  
          }catch (Exception e) {
              Log.e("TCP", "C: Error", e);
  
          }
      }
  
      public String sendMessage(String message) {
          if (mBufferOut != null &amp;&amp; !mBufferOut.checkError()) {
              mBufferOut.println(message);
              try {
                  if(mBufferIn.ready() == true){
                      String line;
                      if((line = mBufferIn.readLine()) != null){
                          return line;
                      }
                      else{
                          return "NULL";
                      }
  
                  }
                  else{
                      return "NOT READY";
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return "ERROR";
      }
  
      public void closeConnection(){
          try {
              socket.close();
          }catch (IOException e){
              Log.e("TCP Client", "Error");
          }
      }
  
      @Override
      public void run(){
      }
  }
  </code></pre>