5

I have been working on a project to connect computers located in different locations together through Python. Initially, while testing, I used my private IP address (I did not know it was private at the time) to connect computers on the same network as mine. But as soon as I tried doing this with computers located on different networks in different locations, it simply did not work.

And I assume this is because the program is using the local IP address of my computer that can connect only to computers on the same network. Here are my simplified programs:

Here is my server-side script:

server = socket.gethostbyname(socket.gethostname()) # 10.128.X.XXX which is the Internal IP
print(server)
port = 5555
clients = 0

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((server, port))

s.listen(2)
print("Waiting for connection...")

while True:
    conn, addr = s.accept()
    print("Connected to: ", addr)

    conn.send(str.encode(f"{clients}"))
    clients += 1

and here is my client side-script:

class Network:
    def __init__(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = "10.128.0.2"
        self.port = 5555
        self.addr = (self.server, self.port)
        self.id = int(self.connect())

    def connect(self):
        self.client.connect(self.addr)
        return self.client.recv(2048).decode()

network = Network()
print(f"Connected as client {network.id}")

Now when I tried replacing the private IP address with the global IP address (as specified here: How do I get the external IP of a socket in Python?) I got the following error:

# Getting the Global IP Address

from requests import get
server = get("https://api.ipify.org").text
s.bind((server, port))
OSError: [WinError 10049] The requested address is not valid in its context

I have tried searching a lot on how to communicate (transfer small amounts of data as strings) between multiple computers located in different locations using different networks, but I haven't really gotten a solution. Is there a way that I can do this?

7
  • in server you always use local IP, not external IP. Server has to bind to local network card (NIC - Network Internet Card) or to all of them (when you use 0.0.0.0). And client which want to connect from internet has to use external IP. Client connects to external IP which means IP of Internet Provider router, and router sends/redirects it to your server. Commented Jun 29, 2020 at 5:06
  • So then how would I get the client to use the external IP? like in the server script, I say s.bind(___), but there is no such thing in the client script. Commented Jun 29, 2020 at 5:10
  • 2
    in server you use s.bind(local_IP, port) and in client use s.connect(external_IP, port) Commented Jun 29, 2020 at 5:11
  • Oh wow! Thanks You so much! It actually worked. Perhaps you can post this as an answer so that I can approve it! Commented Jun 29, 2020 at 5:15
  • Hi @BhavyeMathur, Can you please tell me how did you solve this issue ? I am using same technique recommended here, but unable to connect. It throws this error: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it Commented Aug 31, 2020 at 15:52

1 Answer 1

7

In server you always use local IP (it is IP of one of network cards in computer or 0.0.0.0 to use all network cards)

s.bind( (local_IP, port) )

# or 

s.bind( ('0.0.0.0', port) ) 

In client you use external IP

s.connect( (external_IP, port) )

External client uses external IP to connect with your Internet Provider route and this router knows that this external IP is assigned to your computer and it redirects it your server.

At the same time local client can use local IP to connect with the same server.

external_client --> router(external_IP) --> server(local_IP) <-- local_client
Sign up to request clarification or add additional context in comments.

10 Comments

Hi furus, I get my external_IP and used in same way as you mentioned (local_IP in server and externel_IP in client) but not able to connect. Can you guide me further ? (My client and server, both are running in same machine. I just want to check it, before I run client outside the network). It cause this error: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
router controlled by your Internet Provider can block some ports - and it can be problem because you can't chagen its configuration. You can test code with different ports - especially with popular ports used by servers - ie. 80 for HTTP, 443 for HTTPS, 22 for FTP, etc.
you can use online tools to check if your port is accessible when you run server - ie. yougetsignal.com/tools/open-ports or pentest-tools.com/network-vulnerability-scanning/…
in client better use 0.0.0.0 to make sure it use all local networks cards (all local IP)
Thank you furas, my ports are closed. I think port forwarding would solve my problem. Am I right ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.