Itay Tsuk
Itay Tsuk

Reputation: 103

python socket works only locally and on near by laptop

I finished working on a 2 player game with python-socket While I tested it locally on my computer it worked fine, so I tested the code on my laptop but it didn't work(the client couldn't connect to the host) when the laptop was the client and for some reason, it did when the laptop was the client. I tested it with my friend too but both when he was the host and I was- didnt work.

this is the code for the server:

    import socket
    import pickle
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    hostName = input('host name(if you are the host write "host"):')
    if hostName == "host":
        host = True
        hostname = socket.gethostname()
        s.bind((socket.gethostbyname(hostname), 4242))
        print(f"your host name is: {socket.gethostbyname(hostname)}")
    
        s.listen(1)
        conn, addr = s.accept()

    else:
        host = False
        s.connect((hostName,4242))

The error says that the client timeout when it tried to s.connect

Upvotes: 0

Views: 111

Answers (1)

Pravin
Pravin

Reputation: 11

I tried your code on my laptop and it's working correctly. I think it's the firewall

check out this answer: python socket Windows 10 connection times out

Upvotes: 1

Related Questions