简体   繁体   中英

Socket communication with Python on Android

I've installed sl4a and Python on my Android-powered Galaxy Tab. I've run into problems using python's socket module to communicate between my laptop and my phone.

This example 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:

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')

And the code I'm executing on the laptop (where <Tab IP Address > is the IP address of the Tab):

import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('<Tab IP address>', 50111))

The client script (on the laptop) errors with:

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

And the script on the Tab displays all but the last Toast message.

I'm getting the IP address of the Tab from www.whatismyip.org.

Local socket programming (ie connecting to IP address 'localhost' and running both client and server on the Tab) works fine.

I've been using the guide here to transfer files to my Tab.

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!

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.

EDIT AGAIN: From reading this question and this link , 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.

You were right about the "bulk IP" thing. When you're connecting your phone through wi-fi it will get a random IP from the router's available IP address pool.

It usually gets the same IP if you have a constant number of devices connected to the router. For example if you have 3 computers connected through cable and 1 smartphone then the phone will always get the 192.168.1.5 IP (192.168.1.2 - .4 IPs are used for your computers).

Depending on the router you have you can set it up to give the exact same address to your smartphone every time you connect it to wi-fi. And the IP you were using from www.whatismyip.org is the one from the provider, which a network uses to reach "public internet" (but that's another story).

You should check your local IP that's on your tablet (Settings->Wi-Fi->Wi-Fi settings-> and press the connection. A small info box should appear with your IP).

You should use that in your code for the server.

I hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM