2015-12-02 95 views

回答

0

經過長期搜索,答案是沒有。 Asyncore假定底層套接字是面向連接的,即TCP。

2

是的,你可以。這裏有一個簡單的例子:

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

+0

謝謝了'self.sendto(數據(地址,端口))' – moonraker