<p>考虑到我已经用DatagramProtocol在Twisted中实现了一个UDP客户机,并使用它与UDP服务器通信,该服务器一度脱机(由于重新启动-因此它不会更改其IP地址),所以在我的协议中调用stopProtocol,但是Twisted将传输本身设置为None。在</p>
<p>如何解决Twisted中的简单重新连接或重新启动传输?
根据文件,我无法再使用udp连接。在</p>
<p>考虑到在UDP中,即使服务器死了,发送方也应该能够发送数据包,而且协议在数据包中有自己的连接处理,如果传输不会消失,我可以完全通过包层重新连接逻辑部分。在</p>
<p>我想在内核运行时用新协议再次运行listenUDP是行不通的。在</p>
<pre><code>from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
class UDPClientProtocol(DatagramProtocol):
def __init__(self, host, port):
self.host = host
self.port = port
def startProtocol(self):
# Called when transport is connected
self.transport.connect(self.host, self.port)
self.transport.write('initiate protocol') # pseudo code.
def stopProtocol(self):
print "I have lost connection and self.transport is gone!"
# wait some time and try to reconnect somehow?
t = reactor.listenUDP(0, UDPClientProtocol('127.0.0.1', 12345))
reactor.run()
</code></pre>