title: Socket Programming with Python server and Android client
body: <p>I want to setup a socket interface. PC side runs a very simple socket server written in Python to test the connection:</p>
  
  <pre><code>#!/usr/bin/python           # This is server.py file
  
  import socket               # Import socket module
  
  s = socket.socket()         # Create a socket object
  host = socket.gethostname() # Get local machine name
  port = 5000                # Reserve a port for your service.
  s.bind((host, port))        # Bind to the port
  
  s.listen(5)                 # Now wait for client connection.
  while True:
     c, addr = s.accept()     # Establish connection with client.
     print 'Got connection from', addr
     c.send('Thank you for connecting')
     c.close()                # Close the connection
  </code></pre>
  
  <p>An Android client application will connect to PC:</p>
  
  <pre><code>package com.example.androidsocketclient;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.view.View;
  import android.widget.EditText;
  
  public class MainActivity extends Activity {
  
      private Socket socket;
  
      private static final int SERVERPORT = 5000;
      private static final String SERVER_IP = "192.168.2.184";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          new Thread(new ClientThread()).start();
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      public void onClick(View view) {
          try {
              EditText et = (EditText) findViewById(R.id.EditText01);
              String str = et.getText().toString();
              PrintWriter out = new PrintWriter(new BufferedWriter(
                      new OutputStreamWriter(socket.getOutputStream())),
                      true);
              out.println(str);
          } catch (UnknownHostException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      class ClientThread implements Runnable {
  
          @Override
          public void run() {
  
              try {
                  InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  
                  socket = new Socket(serverAddr, SERVERPORT);
  
              } catch (UnknownHostException e1) {
                  e1.printStackTrace();
              } catch (IOException e1) {
                  e1.printStackTrace();
              }
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>However, I cannot establish a connection between PC and Android device. Any idea to fix this?  </p>