Python socket connection on Android

I installed sl4a and Python on my Android version of the Galaxy Tab. I am having problems using the python socket module to communicate between my laptop and my phone.

This example always worked fine for me when I only write programs on a computer just for python, but it doesn't work as expected on a computer / tab. Here is the code that I run 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 execute 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)) 

Client error script (on laptop):

 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 everything except the last Toast message.

I get the IP address of the tab from www.whatismyip.org.

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

I used the guide here to transfer files to my tab.

For context, the ultimate desired goal of my project is to send text through my tab by running a command on my laptop. I am sure that solutions already exist, but I would like to know how to do it myself!

EDIT: [Posted on 03/21/12] It turns out that the above code works fine (communication between Tab and laptop) if I connect Tab to my wifi network and use the address 192.168.0.xx. This makes me think that the problem is that the IP address that I use to connect to the Tab is not the code itself. I will continue to try. Any tips on finding the best IP address for the connection or errors I could make would be appreciated.

EDIT AGAIN: From reading this question and this link that the IP addresses of smartphones (and tablets) are not static, so using a mobile device as a server in this situation is the wrong way. I rethink my program structure. Although now I know that this is the wrong way to structure the interaction, but I still do not quite understand why this failed. I suspect that after reading the previous links, the IP address I obtained from www.whatismyip.org was not unique to my tab, but rather a “mass IP” that belongs to my network provider, from which it can then be redirected to separate devices (in the same way that a network router can redirect connections to itself on separate devices on the network). However, I’m not sure about this and I can’t come up with the slightest way to test it (without waiting for people on the same network as me who live in the same area of ​​cellular communication and ask them about their mobile IP addresses). If someone could clarify this point, I would be very grateful.

+4
source share
1 answer

You were right about mass IP. When you connect your phone via Wi-Fi, it will receive a random IP address from the pool of available IP addresses of the router.

Usually it gets the same IP address if you have a constant number of devices connected to the router. For example, if you have 3 computers connected via cable and 1 smartphone, then the phone will always receive the IP addresses 192.168.1.5 (192.168.1.2 -.4) for your computers.

Depending on which router you have, you can configure it to provide the same address to your smartphone every time you connect to Wi-Fi. And the IP that you used from www.whatismyip.org is the one used by the provider that uses the network to access the "public Internet" (but that's another story).

You must check your local IP address on your tablet (Settings-> Wi-Fi-> Wi-Fi Settings-> and click on the connection. A small information window should appear with your IP address).

You must use this in your server code.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1400863/


All Articles