套接字连接被拒绝:telnet:无法连接到远程主机:连接被拒绝

socket connection refused: telnet: Unable to connect to remote host: Connection refused

sentdex 的帮助下,我一直在自学 python 套接字模块。当我尝试 运行 代码时,没有错误。代码是:

`import socket
from _thread import *

host = 'localhost'
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((host, port))
except socket.error as e:
    print(str(e))

s.listen(5)
print('Waiting for a connection.')
def threaded_client(conn):
    conn.send(str.encode('Welcome, type your info\n'))

    while True:
        data = conn.recv(2048)
        reply = 'Server output: '+ data.decode('utf-8')
        if not data:
            break
        conn.sendall(str.encode(reply))
    conn.close()


while True:

    conn, addr = s.accept()
    print('connected to: '+addr[0]+':'+str(addr[1]))

    start_new_thread(threaded_client,(conn,))
`

当我试图在我的 raspberry pi 上连接它时,它给出了错误: telnet: Unable to connect to remote host: Connection refused 我尝试将主机修改为我在其他问题中读到的无数不同选项,例如 host=''host=127.0.0.1host=0.0.0.0。一切都无济于事;但是,当我在计算机 运行 上尝试 host='localhost' 脚本时,它确实建立了连接。当我试图从我的 raspberry pi 3 对它执行 ping 操作时,它什么也没做。起初,它似乎可以工作,但一段时间后没有任何变化,当我取消它时, raspberry pi 显示没有收到任何包裹。请告诉我可能是什么问题。它可以是语法上的还是基于防火墙的?

我发现了我的错误:我一直在尝试连接到我的路由器的 IP,而不是我的计算机的 IP(我是 运行 套接字服务器)。抱歉打扰了。