I'm new to Networks and socket programming and require help please. I'm following a youtube tutorial and when running the client.py and server.py scripts on my machine, everything works according to the video. But when I try running client.py on different machines (47:30), it doesn't run properly and I get the following error:
client.connect(ADDR)
TimeoutError: [WinError 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
This is my server.py:
import socketimport threading# Server ConfigurationHEADER = 64TCP_PORT = 5050SERVER = socket.gethostbyname(socket.gethostname())ADDR = (SERVER, TCP_PORT)FORMAT = 'utf-8'DISCONNECT_MESSAGE = "!DISCONNECT"server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind(ADDR)def handle_client(conn, addr): print(f"[NEW CONNECTION] {addr} connected.") connected = True while connected: msg_length = conn.recv(HEADER).decode(FORMAT) if msg_length: msg_length = int(msg_length) msg = conn.recv(msg_length).decode(FORMAT) if msg == DISCONNECT_MESSAGE: connected = False print(f"[{addr}] {msg}") conn.send("Msg received".encode(FORMAT)) conn.close()def start(): server.listen() print(f"[LISTENING] Server is listening on {SERVER}") while True: conn, addr = server.accept() thread = threading.Thread(target=handle_client, args=(conn, addr)) thread.start() print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}") print("[STARTING] server is starting...")start()
This is my client.py (The only thing I've changed when running it on different machines, was the hard-coded IP Server address):
import socketHEADER = 64TCP_PORT = 5050SERVER = "my IP address"ADDR = (SERVER, TCP_PORT)FORMAT = 'utf-8'DISCONNECT_MESSAGE = "!DISCONNECT"client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)client.connect(ADDR)def send(msg): message = msg.encode(FORMAT) msg_length = len(message) send_length = str(msg_length).encode(FORMAT) send_length += b'' * (HEADER - len(send_length)) client.send(send_length) client.send(message) print(client.recv(2048).decode(FORMAT))send("Hello This is Koder")input()send("Hello Koder")input()send("Hello Koder")input()send(DISCONNECT_MESSAGE)
I tried changing the IP address to a different one, tried running the server on a different machine and connecting the client that way, checked that all machines are connected to the same Wifi network, and checked the firewall settings for the machine running the server script, but nothing worked. :(