Twisted Python: Max Packet Size? Flush socket?

风格不统一 提交于 2019-12-11 01:45:49

问题


I'm implementing a client-server solution based on Twisted for the server side and e.g. and Android phone for the client side. Because the Andoird emulator takes no TCP Packets larger then 1500b (or less?), I need to be able to chunk packets on the server side. Without flushing the socket after each "transport.write", Twisted buffers the outgoing data so the chunking would be useless without somekind of manual or automatic flushing / maxpacketsize function. How do I do this in Twisted? I'm familiar with the "reactor.doSelect(1)" function, but since I'm using the EPoll reactor (for scalability and performance reasons), I cannot use doSelect. Is it possible to change the maxPacketValue for certain connections within Twisted?

Hoping that someone can show me the light...


回答1:


TCP packets are automatically chunked by the OS, all the application can do is give hints when to flush. Apart from that, an application can just read and write to a stream.

The OSs of two communicating peers will automatically configure the maximum packet sizes based on the MTU on the links with Path MTU discovery. Make sure you don't block ICMP packets to get that to work.

Since it is extremely unlikely that a wrong MTU is the problem (and an MTU of 1500 or less is often set anyway), you should re-diagnose your problem, for example with a packet tracer such as wireshark.




回答2:


TCP is a stream-oriented protocol, not a packet-oriented protocol. When you call transport.write, that data gets appended to the TCP stream and may be sent out in any number of packets; it might be broken up, or glued together with the next or previous call to write. This is a fairly frequently asked question about Twisted, but everyone who asks it asks it slightly differently.

You want to use a protocol construction kit like AMP, or, more basically, LineReceiver, to delimit the messages within your protocol, rather than relying upon the random nature of packet boundaries.



来源:https://stackoverflow.com/questions/7844302/twisted-python-max-packet-size-flush-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!