[Twisted-Python] TCP Packet Size Considerations

Andrew Bennetts andrew-twisted at puzzling.org
Tue Mar 8 23:06:47 MST 2005


On Wed, Mar 09, 2005 at 04:10:21PM +1100, Grant McDonald wrote:
[...]
> 
> class ServerDataConnection(Protocol):
>     def closeConnection(self):
>         self.transport.loseConnection()
>    
>     def dataReceived(self, data):
>         f = file('received.dat', 'w')
>         f.write(data)
>         f.close()
[...]

On Wed, Mar 09, 2005 at 04:56:19PM +1100, Grant McDonald wrote:
> After adding some code to report the transmission size i can confirm that 16837
> bytes are sent and 8077 bytes are received via the previous code.

This is because while TCP is a byte-stream, the data can be delivered in
arbitrarily small pieces, depending on things like the MTU.  Your
ServerDataConnection is buggy, in that it only keeps the most recent chunk
received, rather than the full stream.  A better implementation would be:

class ServerDataConnection(Protocol):
    def closeConnection(self):
        self.transport.loseConnection()
 
    def connectionMade(self):
        self.f = file('received.dat', 'w')
   
    def dataReceived(self, data):
        self.f.write(data)
 
    def connectionLost(self):
        f.close()

See also the "Protocol Design" articles linked from http://itamarst.org/.

-Andrew.





More information about the Twisted-Python mailing list