-1

Whatever tutorials I've read online dictate how to connect two computers on a local network.

What I want to do is connect my Android phone (I have a terminal emulator on it which has Python installed -- Termux), to my computer.

Server --> Android phone --> Internet

Client --> Computer --> Internet

Client code:

import socket
server_ip = "<My phone's ip which I can google on my phone>"
port = 9999

s = socket.socket()
s.connect((server_ip, port))
s.send(str.encode("Hello there!", 'utf-8'))
s.close()

Server code:

import socket

s = socket.socket()
s.bind(('', 9999))
s.listen(1)
conn, addr = s.accept()
print( str(conn.recv(1024), 'utf-8') )

The thing is, if I try this, I get the TimeoutError.

(PC-side code)

>>> import socket
>>> s = socket.socket()
>>> s.connect(('72.128.66.21', 9999))
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    s.connect(('72.128.66.21', 9999))
TimeoutError: [WinError 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

It looks like the IP I entered was wrong, so I re-checked it, it wasn't. The server was hosted on that IP and port.

Also, I don't think Termux is like some sort of virtual environment, so the server should actually be publicly accessible.

EDIT: If this is of any help, I am using portable hotspot of another phone as an access to the Internet, so it's acting like a router.

EDIT 2: Also, I tried doing telnet myPhone'sIP portNo and it failed.

zombiesauce
  • 1,009
  • 1
  • 7
  • 22
  • Describe what happens and show possible error messages as properly formatted text in the question. – Michael Butscher Nov 16 '19 at 15:20
  • @Michael Okay, did that. – zombiesauce Nov 16 '19 at 15:28
  • On Windows command line you can try to "ping" the IP and use a terminal program to connect to the port on the phone. – Michael Butscher Nov 16 '19 at 15:32
  • @MichaelButscher I tried pinging the IP and it works. So it is accessible from outside then. – zombiesauce Nov 16 '19 at 15:38
  • 2
    Are you trying to connect using a local or public IP address? Does it start with 192 or something else? – Carcigenicate Nov 16 '19 at 15:42
  • @Carcigenicate No I'm trying to use the public IP address. It doesn't start with 192.168.43.xx, I'm trying to connect via the internet so I think I would use the public IPv4. – zombiesauce Nov 16 '19 at 15:50
  • @Carcigenicate I'm sorry I'm not familiarised with port forwarding. I don't know what that is. Needless to say then I haven't handled it. – zombiesauce Nov 16 '19 at 16:13
  • 1
    Are you sure you copied the python session correctly? You have an attempt to connect to '72.128.66.21' but the error suggests you were trying to connect to '192.168.43.0'. So which is it? Also, what does *google my phone's IP* mean? – President James K. Polk Nov 16 '19 at 17:10
  • @JamesReinstateMonicaPolk I'm sorry about that, I tried to recreate the error, I'll change the IP in there to avoid confusion. – zombiesauce Nov 18 '19 at 02:02
  • @JamesReinstateMonicaPolk I will Google the IP of my phone ON my phone itself on Chrome or something. – zombiesauce Nov 18 '19 at 02:03
  • I don't even understand what "Google the IP of my phone" might mean? – President James K. Polk Nov 18 '19 at 02:06
  • Ok, thank you for clarifying. But, in fact, google does not give you the IP of your phone in most cases. Mostly it gives you the public IP of the last NAT device between you and a google. Your IP is different and is usually a non-routable address from one of the reserved blocks. – President James K. Polk Nov 22 '19 at 17:51

2 Answers2

-1

blackapps answered my question in the middle of his question bank.

I am indeed using my SIM card for the internet access on my phone. If he's correct, that'd mean that my phone can't have requests from other devices coming in first. I'd have to establish a connection with them on my side. Therefore, I cannot act as a server. This question was therfore not related to Python at all, and should be closed.

zombiesauce
  • 1,009
  • 1
  • 7
  • 22
-2

server_ip = "<My phone's ip which I can google>"

On the client you google the ip address of the server? How would you do that?

Server --> Android phone --> Internet

It the servers phone connected with the internet through wifi and a router? Or using a simcard?

If simcard then what you want is imposible as the cellphone provider does not allow running servers on the phone... well you can run a server but incoming connections are blocked.

I am using portable hotspot of another phone as an access to the Internet, so it's acting like a router.

It is unclear if you would use that hotspot for the phone where your server is running on. If so then it will not work as incoming connections to that hotspot-phone are blocked by the provider.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • 1
    It is better to use comments to gather more information from the OP. This answer consists mostly of questions. – greeble31 Nov 16 '19 at 18:58
  • `This answer consists mostly of questions.` This answer was the answer. Only you did not see it blinded by questionmarks. @greeble31 – blackapps Nov 18 '19 at 11:00