Dear all,<br><br>I'm using the Twisted to write a socket server. I has a problem in the socket server. I could not receive a big size data when the data size is bigger then the connection <span style="font-weight: bold;">
buffersize.<br><span style="font-weight: bold;"><span style="font-weight: bold;"><br>The protocol description:<br><br><span style="font-weight: bold;"><span style="font-weight: bold;">First, </span>Client send data head to Server.
</span> Head Format: <span style="color: rgb(255, 0, 0);">ABC,123456,XXX,XXX,XXX^</span><br><span style="font-weight: bold;">Second, Client send big data to Server, the big data is stream data.<span style="font-weight: bold;">
<br>After Server received the big data and then save it to database.<br></span></span></span><br></span></span>My problem source code :<br>================================================================================================
<br><div style="margin-left: 40px;"><span style="color: rgb(51, 51, 255);">class MyProtocol(protocol.Protocol):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> is_big_data = False</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> big_data = ''</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> big_data_size = 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
def connectionMade(self):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.big_data = ''</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
self.big_data_size = 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.is_big_data = False</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
pass</span><br style="color: rgb(51, 51, 255);"><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> def dataReceived(self, data):</span><span style="color: rgb(51, 51, 255);">
<br><br style="color: rgb(51, 51, 255);"></span><span style="color: rgb(51, 51, 255);"> if check_head_ok(data):<br> </span><span style="color: rgb(51, 51, 255);"><span style="font-style: italic; color: rgb(0, 51, 0);">
<span style="font-style: italic;"></span># To receive the big size data</span></span><br><span style="color: rgb(51, 51, 255);"> if </span><span style="color: rgb(51, 51, 255);">self.is_big_data:</span><span style="color: rgb(51, 51, 255);">
<span style="font-style: italic; color: rgb(0, 51, 0);"></span></span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return <br style="color: rgb(51, 51, 255);">
</span><span style="color: rgb(51, 51, 255);"> <br><span style="font-style: italic; color: rgb(0, 102, 0);"> <span style="color: rgb(0, 51, 0);"># The big data will overflow the Twisted receive buffer size, so loop to receive data
</span></span><br style="color: rgb(51, 51, 255);"></span><span style="color: rgb(51, 51, 255);"> if is_big_size_data:</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> big_data += data
</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> if big_data_size == len(big_data):
</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> # To save data to database</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> ....
</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> pass </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span>
<span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> def connectionLost(self, reason):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
self.big_data = ''</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.big_data_size = 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
self.is_big_data = False</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> pass</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> def check_head_ok(self, head):</span><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
if data.startswith('ABC'):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> values = data.split(',')</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> self.big_data_size = int(values[1])</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.is_big_data = self.big_data_size
> 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return True</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> else:</span>
<br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return False </span> <br></div>================================================================================================
<br><br>If you know it, Please tell me. Thank you very much! :)<br><div style="margin-left: 40px;"><br></div><span style="font-weight: bold;"></span><br>Beat Regards,<br>Steven. Wang<br><br><br><br>