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