我可以在asyncore中編寫UDP客戶端/服務器應用程序嗎?我已經寫了一個使用TCP。我的願望是將其與UDP的支持集成。使用UDP的Python asyncore
我的問題沒有以前問/由以下回答: Python asyncore UDP server
我可以在asyncore中編寫UDP客戶端/服務器應用程序嗎?我已經寫了一個使用TCP。我的願望是將其與UDP的支持集成。使用UDP的Python asyncore
我的問題沒有以前問/由以下回答: Python asyncore UDP server
經過長期搜索,答案是沒有。 Asyncore假定底層套接字是面向連接的,即TCP。
是的,你可以。這裏有一個簡單的例子:
class AsyncoreSocketUDP(asyncore.dispatcher):
def __init__(self, port=0):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.bind(('', port))
# This is called every time there is something to read
def handle_read(self):
data, addr = self.recvfrom(2048)
# ... do something here, eg self.sendto(data, (addr, port))
def writable(self):
return False # don't want write notifies
這應該足以讓你開始。看看asyncore
模塊的更多想法。
小注:asyncore.dispatcher
將套接字設置爲非阻塞。如果 想要將大量數據快速寫入套接字而不會導致 錯誤,則必須執行一些與應用程序相關的緩衝操作 ala asyncore.dispatcher_with_send
。
感謝這裏讓我的(有些不太準確)的代碼開始: https://www.panda3d.org/forums/viewtopic.php?t=9364
謝謝了'self.sendto(數據(地址,端口))' – moonraker