TCP packets merged at network level

六眼飞鱼酱① 提交于 2019-12-06 06:31:42

问题


Does anybody knows how and why a counter party would receive TCP packets merged instead of individually packages? I already set TCP Nodelay to true at socket level, but tcpdump still sees some packets as merged. After 4 successful packets sent with size of 310 bytes, I got 3 x 1400 bytes instead of 15 x 310 bytes. This is causing some important latency. Thanks.

http://www.2shared.com/photo/_bN9UEqR/tcpdump2.html

s = new Socket(host, port);
s.setTcpNoDelay(true);
s.getOutputStream().write(byteMsg); 
s.getOutputStream().flush()

回答1:


TCP is a stream-based protocol. It doesn't preserve boundaries with respect to send/recv calls. The only thing guaranteed is that the concatenation of send's will be the same as the concatenation of recv's (under normal circumstances).

If you're implementing a custom protocol and need some way to split the data into multiple logical messages, you need an encoding for that.

A simple encoding is to encode each message as a 32-bit unsigned integer denoting the length of the message payload, followed by the actual message payload. Then, on the receiving side, properly decode the input according to this encoding. To do that, you will need a buffer that will store a partially received message. If manipulating raw integers is a problem, you can encode the length some other way, e.g. as a decimal number followed by a newline.




回答2:


Coalescing can occur in many places

  • sender application buffering
  • sender OS buffering
  • sender network adapter buffers
  • router buffers
  • receiver network adapter buffers
  • receiver OS buffering
  • receiver application buffers/queues

It appears from what you have said there is coalescing between the senders network adapter and the receiver's OS. (As tcp-no-delay instructs the OS not to buffer and tcpdump reads before the application)




回答3:


You can try to enable the TCP_NODELAY option on the used socket (setTcpNoDelay() method).

It is by default disabled which means that the transmitted data is optimized for a minimum number of packages sent (see Nagle's algorithm).




回答4:


Does anybody knows how and why a counter party would receive TCP packages merged instead of individually packages?

Because that is what TCP is specifically designed to do.



来源:https://stackoverflow.com/questions/11241430/tcp-packets-merged-at-network-level

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