[Twisted-Python] TCP Packet Size Considerations

Grant McDonald gmcdonald at infocomp.com
Tue Mar 8 22:10:21 MST 2005


JP wrote:

> There are no such known limits.  I might even go as far to say that there
> are no such limits, if I were feeling daring. :)  Do you have a short
> program which demonstrates this behavior that you can share?  
> Perhaps after looking at that someone can suggest the cause of the
disconnects.

After looking into it some more myself i realised an uncaught exception was
dropping the connection but the after cutting my test code back to minimal
functionality the inconsistency with received data was still there. The last
4KB of the data was received and the first 14KB discarded somewhere!

I have created a minimal example from my initial test code. Start the
simpleserver with twistd.py -y simpleserver.py and just run the
simpleclient.py the builder.dat file is just a nonsense file that is about
18KB in size. Testing on my machine produced a received.dat file which was
4KB in size. I am currently using Python 2.3.3 and Twisted 1.3.0. The code
is as follows (and is also attached for convenient running):

### ------------ simpleclient.py ---------------------------------

from twisted.internet.protocol import Protocol
import twisted.internet.error
from simpleprotos import SimpleClientFactory
factory = SimpleClientFactory()

# open a large nonsense file to send (18KB)
f = file('builder.dat')
data = f.read()
print len(data)
f.close()

from twisted.internet import reactor
reactor.connectTCP('127.0.0.1', 10000, factory)
reactor.callLater(1.0, factory.sendMessage, data)
reactor.run()

### ------------- end ---------------------------------------------

### ------------ simpleserver.py ----------------------------------

from twisted.application import internet # services that run TCP/SSL/etc.
from twisted.application import service
from simpleprotos import ServerDataConnection, SimpleFactory

def makeService():
    factory = SimpleFactory() # here we create a ConnectionManager
    return internet.TCPServer(10000, factory)

application = service.Application("Connection Manager")
sc = service.IServiceCollection(application)
sc.addService(makeService())

### ------------- end ---------------------------------------------

### ------------ simpleprotos.py ----------------------------------

from twisted.internet.protocol import ClientFactory, Factory, Protocol

class SimpleClientFactory(ClientFactory):
    def buildProtocol(self, addr):
        self.p = ClientDataConnection()
        self.p.factory = self
        return self.p
    
    def sendMessage(self, data):
        if hasattr(self, 'p'):
            self.p.sendData(data)

class SimpleFactory(Factory):
    def buildProtocol(self, addr):
        self.p = ServerDataConnection()
        self.p.factory = self
        return self.p

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


class ClientDataConnection(Protocol):
    def closeConnection(self):
        self.transport.loseConnection()
    
    def sendData(self, data):
        self.transport.write(data)

### ------------- end ---------------------------------------------

Regards,

Grant McDonald

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20050309/8f93b6d6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simpleserver.py
Type: application/octet-stream
Size: 438 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20050309/8f93b6d6/attachment-0008.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simpleprotos.py
Type: application/octet-stream
Size: 910 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20050309/8f93b6d6/attachment-0009.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simpleclient.py
Type: application/octet-stream
Size: 417 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20050309/8f93b6d6/attachment-0010.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: builder.dat
Type: application/octet-stream
Size: 16837 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20050309/8f93b6d6/attachment-0011.obj>


More information about the Twisted-Python mailing list