0
我的服务器做工精细用我的选择功能:组播与选择
readable, writable, exceptional = select.select(inputs, outputs, timeout)
下面是可写的循环代码:
for s in writable:
try:
next_msg = message_queues[s].get_nowait()
except Queue.Empty:
# No messages waiting so stop checking for writability.
print >>sys.stderr, 'output queue for', s.getpeername(), 'is empty'
outputs.remove(s)
else:
print >>sys.stderr, 'sending "%s" to %s' % (next_msg, s.getpeername())
s.send(next_msg)
s是套接字对象。这里代码只发送给正在发送内容的客户端。 我只是想多播next_msg彼此writables插座,所以我试过了:
for s in writable:
try:
next_msg = message_queues[s].get_nowait()
except Queue.Empty:
# No messages waiting so stop checking for writability.
print >>sys.stderr, 'output queue for', s.getpeername(), 'is empty'
outputs.remove(s)
else:
for multicast in writable:
print >>sys.stderr, 'sending "%s" to %s' % (next_msg, multicast.getpeername())
multicast.send(next_msg)
但是,这并不工作,它总是只发送给发件人。
你是什么意思你的服务器与选择功能正常工作?你有没有测试过输出数组是正确的?它看起来像你的问题在那里。除非你100%确定它正在工作,否则你可以请包括该功能的代码吗? – Sheena
@Sheena你好,谢谢你的帮助。我的意思是说,我有一台服务器可以与多客户端一起工作,无需阻塞和没有线程,因为我使用了select功能。但是,服务器仅向正在发送数据包的客户端发送数据包。我试图修改它以像聊天那样有一个多播服务器:每个客户端都接收其他的数据包。但是当我写了一个for语句来执行多播时,它不起多播的作用:服务器只发送给客户发送者:就像一个镜像,就像我写了任何东西......我不知道如何调试这个..我想我会尝试建立一个全球队列 – Defcode