user3971381
user3971381

Reputation:

Sockets are not working over wifi networks Python

I have made a chat program. It works if the computers are connected tot eh same wifi network. But if they are on separate wifi networks I get this error:

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
  1. Why am I getting this error?
  2. How would I fix this?

Server script:

def startServer(self):
    self.host = "localhost"
    self.port = 8888
    self.s = socket.socket()
    self.s.bind((self.host, self.port))
    self.s.listen(4)

    chatArea = self.chatArea
    chatArea.configure(state=NORMAL)
    chatArea.insert(END, "The server has started!\n\n")
    chatArea.configure(state=DISABLED)

    for i in range(4):
        Thread(target=self.connectClient).start()

def connectClient(self):
    conn, addr = self.s.accept()

    data = conn.recv(1024)
    data = str(data)

    self.connections.append(conn)
    self.names.append(data)

    name = self.names.index(data)

    while str(data) != "Close":
        data = conn.recv(1024)

        if not data:
            break

        data = str(data)

        if data != "Close":
            string = self.names[name] + ": " + data + "\n"

            self.chatArea.configure(state=NORMAL)
            self.chatArea.insert(END, string)
            self.chatArea.configure(state=DISABLED)

            self.sendToClients(self.names[name], data)

    self.connections.remove(conn)
    conn.close()

Here is the client script:

def connect(self):
    self.host = "The other computer ip address"
    self.port = 8888
    self.s = socket.socket()
    self.s.connect((self.host, self.port))

Upvotes: 0

Views: 1138

Answers (2)

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

I've had the exact same issue countless times when I was building a little remote controlled device using a Raspberry Pi. Unfortunately there are only two solutions, both of which are a pain.

You can either port forward the router connected to the device you are trying to connect to, which you will need to do every time it is reset if it is not a static one. You will also then need to connect to the IP of said router instead of your target computer, and the port forwarding should do the rest.

The alternative is to use Logmein Hamachi on both devices, however this is a.) not always possible depending on what system you are using, and b.) has a habit of becoming unreliable. One way or another, there is no way to just do it with coding in python as far as I am aware.

Upvotes: 0

Jason Foster-Bey
Jason Foster-Bey

Reputation: 11

The most likely reason is the lack of any route between the internal IP addresses on each wifi network. Remember, the IP addresses on most wifi nets are local to the net.

Upvotes: 1

Related Questions