[Twisted-Python] Receiving large packets with TCP
dguaraglia at gmx.de
Mon Jul 11 08:19:47 EDT 2005
As Christopher said, you never know how much data you'll receive so you
must buffer the data until you have what you expect. If you know your
packets are going to be _exactly_ 16kb long, then you just read until
you have 16kb+ of data in a buffer, and process that. Then you save the
rest for the next packet. One of the best ways to do that is using an
string, or a queue. I have some example code in case you are interested,
Stefan Näwe wrote:
>I'm using Twisted (2.0.1) for a TCP-based receiver.
>That receiver receives different telegrams sent by another process.
>Those telegrams are sent on different ports (i.e. one telegram type
>is sent on one port). The sizes of the telegram vary from ~16 bytes
>to ~8 kB.
>Everything works perfectly. I've really never been *that*
>I'm having trouble receiving the whole contents of large telegrams.
>For example one telegram is exactly 7452 bytes long but I only get
>My twisted receiver uses the reactor to setup the different ports
>to listen on and creates 'message handler' objects on connect.
>Those message handler objects 'dissect' the received telegram and
>print some of its contents to STDOUT.
>Some parts of my code:
> def dataReceived(self, data):
> if __debug__: log.msg("Received %d bytes" % len(data))
> # here the message handler gets created
> m = self.factory.msg[self.port](data)
> except KeyError:
> # can this really happen ??
> log.msg("Unhandled connection from port %d" % self.port)
> def connectionMade(self):
> addr = self.transport.getHost()
> self.proto = addr.type
> self.peer = addr.host
> self.port = addr.port
> log.msg("Connection from %s" % self.transport.getPeer())
>My 'main' function simply does 'reactor.listenTCP(port, MyFactory)'
>So what do I need to do to get the whole telegram 'in one shot' ?
>Do I need to re-assemble the telegrams myself ?
>(I hope this Information is enough...Tell me, if not.)
More information about the Twisted-Python