udp没有连接的概念,udp套接字可以从网络上的任何服务器接收数据报,并将数据报发送到网络上的任何主机。数据报可以以任意顺序到达 or 根本不会到达 or 在传输过程中被复制。
由于没有连接,所以对于每个udp套接字我们只能使用一个对象,一个协议。然后使用已经定义的接口twisted.internet.interfaces.IReactorUDP
class IReactorUDP(Interface):
"""
UDP socket methods.
"""
def listenUDP(
port: int, protocol: "DatagramProtocol", interface: str, maxPacketSize: int
) -> "IListeningPort":
"""
Connects a given L{DatagramProtocol} to the given numeric UDP port.
@param port: A port number on which to listen.
@param protocol: A L{DatagramProtocol} instance which will be
connected to the given C{port}.
@param interface: The local IPv4 or IPv6 address to which to bind;
defaults to '', ie all IPv4 addresses.
@param maxPacketSize: The maximum packet size to accept.
@return: object which provides L{IListeningPort}.
"""
twisted中已经帮忙实现了udp协议:twisted.internet.protocol.DatagramProtocol
@implementer(interfaces.ILoggingContext)
class DatagramProtocol(AbstractDatagramProtocol):
"""
Protocol for datagram-oriented transport, e.g. UDP.
@type transport: L{None} or
L{IUDPTransport<twisted.internet.interfaces.IUDPTransport>} provider
@ivar transport: The transport with which this protocol is associated,
if it is associated with one.
"""
def logPrefix(self):
"""
Return a prefix matching the class name, to identify log messages
related to this protocol instance.
"""
return self.__class__.__name__
def connectionRefused(self):
"""
Called due to error from write in connected mode.
Note this is a result of ICMP message generated by *previous*
write.
"""
class AbstractDatagramProtocol:
"""
Abstract protocol for datagram-oriented transports, e.g. IP, ICMP, ARP,
UDP.
"""
transport = None
numPorts = 0
noisy = True
def __getstate__(self):
d = self.__dict__.copy()
d["transport"] = None
return d
def doStart(self):
"""
Make sure startProtocol is called.
This will be called by makeConnection(), users should not call it.
"""
if not self.numPorts:
if self.noisy:
log.msg("Starting protocol %s" % self)
self.startProtocol()
self.numPorts = self.numPorts + 1
def doStop(self):
"""
Make sure stopProtocol is called.
This will be called by the port, users should not call it.
"""
assert self.numPorts > 0
self.numPorts = self.numPorts - 1
self.transport = None
if not self.numPorts:
if self.noisy:
log.msg("Stopping protocol %s" % self)
self.stopProtocol()
def startProtocol(self):
"""
Called when a transport is connected to this protocol.
Will only be called once, even if multiple ports are connected.
"""
def stopProtocol(self):
"""
Called when the transport is disconnected.
Will only be called once, after all ports are disconnected.
"""
def makeConnection(self, transport):
"""
Make a connection to a transport and a server.
This sets the 'transport' attribute of this DatagramProtocol, and calls the
doStart() callback.
"""
assert self.transport == None
self.transport = transport
self.doStart()
def datagramReceived(self, datagram: bytes, addr):
"""
Called when a datagram is received.
@param datagram: the bytes received from the transport.
@param addr: tuple of source of datagram.
"""
udp调用connect有什么作用(转) - 林枫水湾湾 - 博客园
udp使用connect建立连接与普通udp连接区别_技术联盟-CSDN博客
Twisted实现UDP服务 - 简书