我可以在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