(python) Socket does not connect to external network

Asked

Viewed 241 times

0

I’m trying to chat at python, however server only connects with the client.py if the client’s machine is connected on the same network. How can I make the two connect to different networks (remotely)? Follow the code server.py:

import socket
import subprocess
import threading


#IP = "localhost"
PORT = 4444 

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', PORT)) #server.bind((IP, PORT))

server.listen(1)
print("[+] Serv startado")


client, client_addr = server.accept()
print(f" {client_addr} O cliente abriu o chat")

def enviar():
    global client, client_addr

    while True:
        msg = input("MSG>>> ")
        msg = msg.encode()
        client.send(msg)
        print("Mensagem enviada!")

def receber():
    global client, server

    while True:
        msg2 = client.recv(1024)
        msg2 = msg2.decode(encoding = 'UTF-8',errors = 'ignore')
        print("\nOutput: ", msg2)
    

threading.Thread(target=enviar).start()
threading.Thread(target=receber).start()

And now, client.py:

import socket 
import subprocess
import threading


SERV_IP = socket.gethostname()
#SERV_IP = "192.168.1.105" 
SERV_PORT = 4444

back = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
back.connect((SERV_IP, SERV_PORT))

def receber():
    global back
    while True:
        msg = back.recv(1024)
        msg = msg.decode() #encoding = 'UTF-8',errors = 'ignore'
        print("\nOutput: ", msg)
        

def enviar():
    global back
    while True:
        msg2 = input("MSG>>> ")
        msg2 = msg2.encode()
        back.send(msg2)
        print("Mensagem enviada!")

threading.Thread(target=receber).start()
threading.Thread(target=enviar).start()

Des de ja, thank you!

1 answer

0

The problem is that server.py doesn’t know the way to the client.py if they’re not on the same network.

Open your command prompt and run the following command:

tracert google.com

You will notice that there are several IP addresses coming out of this your internet provider, going through the routers and other servers until you reach the final IP. Copy that last IP and put it in your browser, you will see that will open the Google page. This is the public IP of Google, you can access the site because you know your ip.

To connect your server.py to your client.py you can do a few things.

You can use Hamachi, it is a computer program that simulates a local network.

Or you should put your server.py file hosted on some server on the web, then make the client.py connect to the server.py using the server’s public ip. You can only do this kind of thing if you have a public ip.

Another thing you can try to do is use a technique called tcp or udp hole Punching, search by these terms.

Which goes something like this: You have a Server, an A Client and a B Client. Client A connects to the Server. Client B connects to the Server. The Server sends to Client A the IP of Client B. The Server sends to Client B the IP of Client A. Client A takes this IP and connects with Client B. Client A and B disconnect from Server.

Client A and B may be on your local networks, but Server needs to be on a public network. Torrent files work this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.