[Twisted-Python] UDP with multiple connections

Jean-Paul Calderone exarkun at divmod.com
Tue Oct 9 20:00:43 EDT 2007


On Wed, 10 Oct 2007 00:37:02 +0100, Phil Mayers <p.mayers at imperial.ac.uk> wrote:
>On Tue, 2007-10-09 at 21:21 +0100, Simon Pickles wrote:
>> Ok, UDP is new to me, so please be gentle....
>>
>> I've got a server authentication app which needs to accept many client
>> connections, but also dispatch messages to a master server. Am I best
>> using unconnected UDP?
>
>You are *best* using TCP. Very few applications are actually suited to
>UDP. UDP:
>
> * has no connection state
> * has no flow control
> * is unfriendly to networks (really the same as the previous point)
> * has no keepalives
> * has problems with MTU and fragmentation for messages > ~1400 bytes
> * is subject to trivial spoofing
> * has no message sequencing
> * is hard to run crypto over (SSL over TCP == trivial)
>
>...and so on.
>
>> do i then have to deal with each received
>> datagram by checking which host is has come from and acting accordingly?
>
>Twisted's UDP support is all "unconnected". All DatagramProtocol
>instances get a call to:
>
> def datagramReceived(self, data, addr)
>
>...where "addr=(ip,port)" for IPv4
>
>Similarly, you would do:
>
>  self.transport.write(bytes, addr)
>
>So, unconnected UDP is your *only* option, because that's how Twisted
>does it.
>
>However, so-called "connected" UDP is really just a way of saving the
>destination address on the socket. There's no *actual* connection
>involved.
>

Everything else here is right, but one correction: Twisted does support
"connected" UDP, via IUDPTransport.connect().  This is indeed little more
than a convenience API, though.  You get to skip passing the address
argument to transport.write() if you use it, and you _may_ receive
connectionFailed notification (which, for example, tells you if you are
sending packets to a port where no application is listening for them),
if all the involved routers decide to cooperate (they frequently will not).

Jean-Paul




More information about the Twisted-Python mailing list