title: Create a python server to send data to Android app using socket
body: <p>I am trying to create a simple chat server program using socket in python ( my pc ) to communicate with my Android client code ( my Android phone ) .</p>
  <p>I have a simple server code which receives messages but it blocks the client app and crashes when I try to send messages from server to client.</p>
  <p>The client code is based on this tutorial: <a href="http://android-er.blogspot.ca/2014/08/simple-android-chat-application-client.html" rel="nofollow noreferrer">Simple Android Chat Application, client side.</a></p>
  <p>Client code:</p>
  <pre><code>    private class ChatClientThread extends Thread {
  
      String name;
      String dstAddress;
      int dstPort;
      
      String msgToSend = &quot;&quot;;
      boolean goOut = false;
  
      ChatClientThread(String name, String address, int port) {
          this.name = name;
          dstAddress = address;
          dstPort = port;
      }
  
      @Override
      public void run() {
          Socket socket = null;
          DataOutputStream dataOutputStream = null;
          DataInputStream dataInputStream = null;
  
          try {
              socket = new Socket(dstAddress, dstPort);
              dataOutputStream = new DataOutputStream(
                      socket.getOutputStream());
              dataInputStream = new DataInputStream(socket.getInputStream());
              dataOutputStream.writeUTF(name);
              dataOutputStream.flush();
  
              while (!goOut) {
                  if (dataInputStream.available() &gt; 0) {
                      msgLog += dataInputStream.readUTF();
  
                      MainActivity.this.runOnUiThread(new Runnable() {
  
                          @Override
                          public void run() {
                              chatMsg.setText(msgLog);
                          }
                      });
                  }
                  
                  if(!msgToSend.equals(&quot;&quot;)){
                      dataOutputStream.writeUTF(msgToSend);
                      dataOutputStream.flush();
                      msgToSend = &quot;&quot;;
                  }
              }
  
          } catch (UnknownHostException e) {
              e.printStackTrace();
              final String eString = e.toString();
              MainActivity.this.runOnUiThread(new Runnable() {
  
                  @Override
                  public void run() {
                      Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();
                  }
                  
              });
          } catch (IOException e) {
              e.printStackTrace();
              final String eString = e.toString();
              MainActivity.this.runOnUiThread(new Runnable() {
  
                  @Override
                  public void run() {
                      Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();
                  }
                  
              });
          } finally {
              if (socket != null) {
                  try {
                      socket.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
  
              if (dataOutputStream != null) {
                  try {
                      dataOutputStream.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
  
              if (dataInputStream != null) {
                  try {
                      dataInputStream.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
  
              MainActivity.this.runOnUiThread(new Runnable() {
  
                  @Override
                  public void run() {
                      loginPanel.setVisibility(View.VISIBLE);
                      chatPanel.setVisibility(View.GONE);
                  }
                  
              });
          }
  
      }
      
      private void sendMsg(String msg){
          msgToSend = msg;
      }
      
      private void disconnect(){
          goOut = true;
      }
  }
  </code></pre>
  <p>Server code:</p>
  <pre><code>import socket
  
  s = socket.socket()
  host = &quot;192.168.1.82&quot;
  port = 8080
  
  s.bind((host, port))
  print host
  s.listen(5)
  c = None
  
  while True:
     if c is None:
         # Halts
         print '[Waiting for connection...]'
         c, addr = s.accept()
         print 'Got connection from', addr
     else:
         # Halts
         print '[Waiting for response...]'
         print c.recv(1024)
  </code></pre>
  <p>When add following two lines to send messages then it doesn't work.</p>
  <pre><code>   # Halts
     print '[Waiting for response...]'
     print c.recv(1024)
  </code></pre>
  <blockquote>
  <pre><code>  q = raw_input()
    c.send(q)
  </code></pre>
  </blockquote>
  <p>Any ideas on how to fix it?</p>