简体   繁体   中英

Simple TCP Server in Python doesn't work online

I'm following this tutorial since I don't know very much about TCP or networking in general, and it works on localhost, but when I switch it to my real IP and ask my friend to try out the client on his system, it doesn't connect, giving the error:

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

If its important here because of the "WinError" part, I'm on Ubuntu, my friend is on Windows.

server.py

import socket

HOST = 'my ip'
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

client.py

import socket

HOST = '86.184.147.101'
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

print('Received', repr(data))

I think its probably just something obvious, but I can't find any relevant questions on stackoverflow or other parts of the internet. Cheers!

One of the possible issues is that you don't have port forwarding enabled on your home router/firewall. Make sure your home router is forwarding port 65432 to your local computer where you run the server. Google "port forwarding" for your router model to find how to configure it.

Check if you are able to ping to the server using the ping command. Check the DNATing rule for your computer/server. Also, check if your firewall is allowing other devices to connect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM