我要做的基本上是使用线程模拟客户端-服务器-客户端聊天。这是到目前为止的代码:
from socket import *
from threading import Thread
import threading
import time
from random import randint
clients = []
HOST = 'localhost'
PORT = 8000
class Server():
def __init__(self):
self.addr = (HOST,PORT)
global clients
self.start()
for i in range(5): Thread(target=self.clientHandler).start()
self.s.close()
def clientHandler(self):
conn, addr = self.s.accept()
clients.append(addr)
print addr[1], "is Connected"
while 1:
time.sleep(5)
# message=repr(data)
# message=message[1:-1].split(':')
message='test' #for debug
conn.sendto(message, clients[1]) #for debug
# conn.sendto(message[1], clients[int(message[0])])
def start(self):
self.s = socket(AF_INET, SOCK_STREAM)
self.s.bind((HOST, PORT))
self.s.listen(5)
print "Server is running......"
class Client(threading.Thread):
global clients
def sendMessage(self):
if len(clients)>1:
to=randint(0, len(clients)-1)
message = str(to)+':hello from '+self.name
print message
self.ss.send(message)
def receiveMessage(self):
while 1:
reply=self.ss.recv(1024)
if reply != '':
print self.name+" received ", repr(reply)
break
def run(self):
self.ss = socket(AF_INET, SOCK_STREAM)
self.ss.connect((HOST, PORT)) # client-side, connects to a host
self.name=self.ss.getsockname()[1]
while True:
# time.sleep(randint(1,5))
# self.sendMessage()
self.receiveMessage()
server=Server()
client1=Client()
client2=Client()
client3=Client()
client1.start()
client2.start()
client3.start()
这个想法是一个客户端应该一遍又一遍地向另一个随机发送消息(尚未考虑排除自己)。为此,消息的格式为dest: message
,其中是用于从列表dest
中选择客户端的随机索引。clients
经过数小时的“调试”,我发现 sendto() 方法将消息发送到所有客户端,而不仅仅是具有指定地址的客户端(如上面的代码那样)。我用错了吗?
此外,当我让客户发送消息时,他们只会立即收到消息。我究竟做错了什么?
我知道这很混乱,对此感到抱歉。我刚开始使用 Python,并且正在测试多种方法以了解它们的工作原理。
谢谢你。- 蟒蛇菜鸟