Socket library isn't working on two machines

I’m coding a game in python. I decided before coding the game’s multiplayer server I’ll experiment with a smaller server(just to see how server coding is in python) but it seems my program only works on my own windows 10 computer. When I share the same client to my friend(his computer also uses windows 10) then the client did not connect(It’s not that I ran the server after my friend ran client, I started the server first). Anyone know why? The client side returns an error saying it isn’t able to connect to my computer(This is the only info my friend gave, and he isn’t lying either. If the client connected without any issues then I would get a message logging that). Here’s my server’s code:

import socket
from time import sleep
#Default port number: 5555
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def run_server(port=5555):
    print('Booting server...')
    print('|-|-|-',end='')
    sleep(0.05)
    server.bind(('',port))
    print('|-|-|-',end='')
    sleep(0.05)
    server.listen(5)
    print('|-|-|',end='')
    sleep(0.05)
    print('\nServer is running and can be accessed now\n===============================================')
    while True:
        c,addr=server.accept()
        print('recieved connection from: ',addr)
        try:
            c.send(bytes("ip=bytes(input('Welcome. Enter hostname to extract ip from: '),'utf-8')",'utf-8'))
            c.send(bytes('_socket.send(ip)','utf-8'))
            reply=c.recv(1024).decode('utf-8')
        except ConnectionResetError:
            print(addr,'closed connection')
            continue
        try:
            ip=socket.gethostbyname(reply)
        except:
           c.send(bytes('''print("The hostname is either invalid or wasn't found")''','utf-8'))
           c.send(bytes('_socket.close()','utf-8'))
           continue
        c.send(bytes("print('"+ip+"')",'utf-8'))
        c.send(bytes('close','utf-8'))
run_server()

And the client’s code:

import socket
def run(mode='client'):
    _socket=socket.socket()
    _socket.connect(('192.168.1.3',5555))
    while True:
        command=_socket.recv(1024).decode('utf-8')
        if command=='close':
            _socket.close()
            break
        else:
            exec(command)
while True:
    try:
        run()
    except OSError as e:
        print(e)
        continue

Are you both at the same location (on the same network)?

The IP address of the server is from a range commonly used for internal networks and cannot be routed over the internet.

If you are on the same network: check the local firewall.

We aren’t on same network but this code should connect on two networks, right?

Not necessarily. The client code tries to connect to 192.168.1.3, which is an address from a private use range. Such ranges are commonly used for WiFi networks at home (and businesses as well) with a NAT router for the connection to the internet that translates internal addresses to a public address. The private use addresses cannot be routed over the internet because multiple networks will use the same addresses.

There are ways to create connections between two systems behind different NAT routers (“NAT traversal”), but I don’t know much about this beyond the term. It’s definitely something that’s way to complicated if you are just getting started with network programming.

1 Like

oh ok, thx

1 Like