title: Socket communication with Python on Android
body: <p>I've installed sl4a and Python on my Android-powered Galaxy Tab.  I've run into problems using python's <code>socket</code> module to communicate between my laptop and my phone.</p>
  
  <p>This <a href="http://docs.python.org/library/socket.html#example" rel="nofollow noreferrer">example</a> had always worked fine for me when I'd been writing computer-only python programs, but doesn't work as expected on a computer/Tab set-up.  Here is the code I'm running on the Tab:</p>
  
  <pre><code>import socket, android
  droid=android.Android()
  droid.makeToast('Running...')
  s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind(('', 50111))
  droid.makeToast('Socket has been bound')
  s.listen(1)
  conn, addr = s.accept()
  droid.makeToast('Connection has been accepted')
  </code></pre>
  
  <p>And the code I'm executing on the laptop (where <code>&lt;Tab IP Address</code>> is the IP address of the Tab):</p>
  
  <pre><code>import socket
  s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect(('&lt;Tab IP address&gt;', 50111))
  </code></pre>
  
  <p>The client script (on the laptop) errors with:</p>
  
  <pre><code>socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
  </code></pre>
  
  <p>And the script on the Tab displays all but the last Toast message.</p>
  
  <p>I'm getting the IP address of the Tab from www.whatismyip.org.</p>
  
  <p>Local socket programming (i.e. connecting to IP address 'localhost' and running both client and server on the Tab) works fine.</p>
  
  <p>I've been using the guide <a href="http://code.google.com/p/android-scripting/wiki/RemoteControl" rel="nofollow noreferrer">here</a> to transfer files to my Tab.</p>
  
  <p>For context, the final desired goal of my project is to be able to send a text via my Tab by executing a command on my laptop.  I'm sure solutions already exist, but I'd like to learn how to do it myself!</p>
  
  <p>EDIT: [Added 21/03/12] It turns out that the code above works fine (communicating between Tab and Laptop) if I connect the Tab to my wifi network and use the 192.168.0.xx address.  This leads me to believe that the problem lies with the IP address I'm using to connect to the Tab, rather than the code itself.  I'll keep trying.  Any advice on the best way find the IP address to connect to, or errors I might be making, would be appreciated.</p>
  
  <p>EDIT AGAIN: From reading <a href="https://stackoverflow.com/questions/9370003/do-android-devices-have-a-static-ip-address">this question</a> and <a href="http://www.makeuseof.com/tag/find-ip-address-mobile-smartphone/" rel="nofollow noreferrer">this link</a>, I've concluded that smartphones' (and tablets') IP addresses are not static, and so treating the mobile device as the server in this situation is the wrong way to do it.  I'll rethink my program structure.
  Although I know now that this was the wrong way to structure the interaction, I still don't entirely understand why it failed.  I suspect, from reading the earlier links, that the IP address I was getting from www.whatismyip.org wasn't unique to my Tab, but was rather a "bulk IP" belonging to my network provider, from which connections can then be forwarded on to individual devices (in the same way that a network router can forward connections to itself on to individual devices on the network).  However, I'm not certain of this, and can't think of any way to verify it (short of hunting down people on the same network as me, who live in the same cell area, and asking them their mobile IP's).  If someone could clarify this point, I'd be very grateful.</p>