Python 套接字服务器未在树莓派中连接

Python socket server not connecting in raspberry

我正在编写一个通过 python 套接字聊天的程序。我的 raspberry pi (运行ning raspbian) 有静态 IP 192.168.1.3 和我的电脑 192.168.1.4

我的server.py

import socket
from threading import Thread

def send_msg(client, msg):
    client.send(bytes(msg,"utf-8"))


def listen_send(client1, address, name):
    while True:
        msg = client1.recv(1024).decode()
        if msg != '':
            for c in clients:
                if c != client1:
                    send_msg(c, "%s (%s) "%(name,address)+msg)


number = int(input("Enter number of connections"))
clients = []
addresses = []
names = []

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 25001
s.bind(('192.168.1.3', port))
print ('Socket binded to port 25001')
s.listen(number)
print('Socket is listening')

while len(clients)< number:
    cl, addr = s.accept()
    names.append(cl.recv(32).decode())
    clients.append(cl)
    addresses.append(addr[0])
    print ('Connection from ', addr[0])
    send_msg(cl, "Waiting for other connections")


for i in range(0,len(clients)):
    send_msg(clients[i], "Connection established\nYour ip is %s"%addresses[i])
    Thread(target=listen_send, args=(clients[i],addresses[i],names[i],)).start()

我的client.py

import socket
from threading import Thread


def send(s):
    while True:
        msg = input()
        s.sendall(msg.encode())

def listen(s):
    while True:
        msg = s.recv(1024).decode()
        if msg != '':
            print(msg)


s = socket.socket()
port = 25001
ip = input("Enter ip\n")
s.connect((ip, port))
name = input("Enter your name\n")
s.send(bytes(name, "utf-8"))

while True:
    msg = s.recv(1024).decode()
    print(msg)
    if 'Connection established' in msg:
        break

t1 = Thread(target=send, args=(s,))
t2 = Thread(target=listen, args=(s,))
t1.start()
t2.start()

当我 运行 我的电脑中的服务器只需将 s.bind ip 更改为 192.168.1.4 我就可以从树莓派连接到我的 client.py 但是当我 运行 树莓派中的服务器我无法从我的电脑连接(我收到超时错误:[WinError 10060])

这是防火墙的问题。使用 ufw 允许端口 25001 解决了这个问题。