2

I am trying to follow this question to connect to my laptop from another laptop over the internet. Both laptops are behind different routers and are running Ubuntu. Here is my server side code:

import socket

class Server():
  def __init__(self,Adress=('',5000),MaxClient=1):
      self.s = socket.socket()
      self.s.bind(Adress)
      self.s.listen(MaxClient)
  def WaitForConnection(self):
      self.Client, self.Adr=(self.s.accept())
      print('Got a connection from: '+str(self.Client)+'.')


s = Server()
s.WaitForConnection()

and client side code:

import socket

class Client():
    myIP = "123.456.789.123" # I got my real IP by running "curl https://ipinfo.io/ip"
    def __init__(self,Adress=(myIP,5000)):
        self.s = socket.socket()
        self.s.connect(Adress)

c = Client()

I started up server.py on the 'server laptop' by running python3 server.py and then ran python3 client.py on the 'client laptop' which resulted in this error:

line 7, in __init__
    self.s.connect(Adress)
ConnectionRefusedError: [Errno 111] Connection refused

I'd like to know where I should go from here to resolve this error and create the connection.

5
  • 1
    I got my real IP by running "curl https://ipinfo.io/ip" - The IP you got is your IP over internet. The laptop you are trying to connect is over ethernet / intranet. So the local IP should be used. If you have laptops behind a common router the IP's could be something like 192.168.0.2, 192.168.0.3 etc depending on the subnet config of router. Commented Jun 21, 2022 at 5:53
  • I don't think I can just use local IPs because both computers are behind different routers. Sorry for the lack of clarity there, I updated the question accordingly. One laptop is at my house and the other is at my friends. How can I give both the local IP and internet IP so that connection is possible? Commented Jun 21, 2022 at 16:54
  • 2
    One of you has to configure their router with port forwarding. The other connects to the public IP and a port, that port is forwarded to the private IP and port on the other side of the router. Commented Jun 21, 2022 at 16:59
  • So I assume this is not possible then without port forwarding? and without modifying the router, I can't setup port forwarding? Commented Jun 21, 2022 at 18:24
  • You should either use a central server over internet for connecting both clients, and do the exchange of data with some identifier (like a chat server). Commented Jun 22, 2022 at 4:54

0

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.