我制作了一个服务器,该服务器接收连接和消息,然后将它们发回。计算机可以连接和发送消息,但不会出现在另一台计算机上。
代码如下:
import socket
import threading
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('10.0.0.60', 666))
sock.listen(20)
connections = []
def handler(c, a):
global connections
while True:
data = c.recv(1024)
for connection in connections:
connection.send(bytes(data))
if not data:
connections.remove(c)
c.close()
break
while True:
c, a = sock.accept()
Cthread = threading.Thread(target=handler, args=(c, a))
Cthread.daemon = True
connections.append(c)
print(connections)