<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2657.73">
<TITLE>RE: [Twisted-Python] TCP Packet Size Considerations</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>JP wrote:</FONT>
</P>

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

<P><FONT SIZE=2>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!</FONT></P>

<P><FONT SIZE=2>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):</FONT></P>

<P><FONT SIZE=2>### ------------ simpleclient.py ---------------------------------</FONT>
</P>

<P><FONT SIZE=2>from twisted.internet.protocol import Protocol</FONT>
<BR><FONT SIZE=2>import twisted.internet.error</FONT>
<BR><FONT SIZE=2>from simpleprotos import SimpleClientFactory</FONT>
<BR><FONT SIZE=2>factory = SimpleClientFactory()</FONT>
</P>

<P><FONT SIZE=2># open a large nonsense file to send (18KB)</FONT>
<BR><FONT SIZE=2>f = file('builder.dat')</FONT>
<BR><FONT SIZE=2>data = f.read()</FONT>
<BR><FONT SIZE=2>print len(data)</FONT>
<BR><FONT SIZE=2>f.close()</FONT>
</P>

<P><FONT SIZE=2>from twisted.internet import reactor</FONT>
<BR><FONT SIZE=2>reactor.connectTCP('127.0.0.1', 10000, factory)</FONT>
<BR><FONT SIZE=2>reactor.callLater(1.0, factory.sendMessage, data)</FONT>
<BR><FONT SIZE=2>reactor.run()</FONT>
</P>

<P><FONT SIZE=2>### ------------- end ---------------------------------------------</FONT>
</P>

<P><FONT SIZE=2>### ------------ simpleserver.py ----------------------------------</FONT>
</P>

<P><FONT SIZE=2>from twisted.application import internet # services that run TCP/SSL/etc.</FONT>
<BR><FONT SIZE=2>from twisted.application import service</FONT>
<BR><FONT SIZE=2>from simpleprotos import ServerDataConnection, SimpleFactory</FONT>
</P>

<P><FONT SIZE=2>def makeService():</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; factory = SimpleFactory() # here we create a ConnectionManager</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; return internet.TCPServer(10000, factory)</FONT>
</P>

<P><FONT SIZE=2>application = service.Application(&quot;Connection Manager&quot;)</FONT>
<BR><FONT SIZE=2>sc = service.IServiceCollection(application)</FONT>
<BR><FONT SIZE=2>sc.addService(makeService())</FONT>
</P>

<P><FONT SIZE=2>### ------------- end ---------------------------------------------</FONT>
</P>

<P><FONT SIZE=2>### ------------ simpleprotos.py ----------------------------------</FONT>
</P>

<P><FONT SIZE=2>from twisted.internet.protocol import ClientFactory, Factory, Protocol</FONT>
</P>

<P><FONT SIZE=2>class SimpleClientFactory(ClientFactory):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def buildProtocol(self, addr):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.p = ClientDataConnection()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.p.factory = self</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.p</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def sendMessage(self, data):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hasattr(self, 'p'):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.p.sendData(data)</FONT>
</P>

<P><FONT SIZE=2>class SimpleFactory(Factory):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def buildProtocol(self, addr):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.p = ServerDataConnection()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.p.factory = self</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.p</FONT>
</P>

<P><FONT SIZE=2>class ServerDataConnection(Protocol):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def closeConnection(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.transport.loseConnection()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def dataReceived(self, data):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f = file('received.dat', 'w')</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.write(data)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f.close()</FONT>
</P>
<BR>

<P><FONT SIZE=2>class ClientDataConnection(Protocol):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def closeConnection(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.transport.loseConnection()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def sendData(self, data):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.transport.write(data)</FONT>
</P>

<P><FONT SIZE=2>### ------------- end ---------------------------------------------</FONT>
</P>

<P><FONT SIZE=2>Regards,</FONT>
</P>

<P><FONT SIZE=2>Grant McDonald</FONT>
</P>

<P><FONT FACE="Arial" SIZE=2 COLOR="#000000"></FONT><FONT FACE="Arial" SIZE=2 COLOR="#000000"></FONT><FONT FACE="Arial" SIZE=2 COLOR="#000000"></FONT><FONT FACE="Arial" SIZE=2 COLOR="#000000"></FONT>&nbsp;

</BODY>
<!--[object_id=#infocomp.com#]--><P><FONT face=Arial color=#808080 size=1>Important notice: This message is intended for the individual(s) and entity(s) addressed. The information contained in this transmission and any attached, may be confidential and may also be the subject of legal privilege, public interest immunity or legal professional privilege. Any review, retransmission, dissemination or other use of, taking of any action in reliance upon this information by person or entities other than the recipient is prohibited and requires authorization from the sender. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person) you may not copy or deliver this message to anyone. In such cases you should destroy this message and kindly notify the sender by reply email. </FONT></P>
<P><FONT face=Arial color=#808080 size=1>WARNING: Although Infocomp has taken reasonable precautions so that no viruses&nbsp;are present in this e-mail, the company cannot accept responsibility for any loss or damage arising from the use of e-mail attachments.</FONT></P></HTML>