开发者

Java socket buffer problem

开发者 https://www.devze.com 2023-02-16 20:59 出处:网络
I\'m currently building a transparent proxy in Java. This transparent proxy is placed between a client and a server, using iptables to redirect the TCP flow.

I'm currently building a transparent proxy in Java. This transparent proxy is placed between a client and a server, using iptables to redirect the TCP flow.

From the poi开发者_Python百科nt of view of the TCP communication, I have the following dialog :

Client                 Server
  | ---- TCP Packet 1 ---> |
  | ---- TCP Packet 2 ---> |
  | <--- TCP Packet 3 ---- |
  | <--- TCP Packet 4 ---- |

From the point of view of the transparent proxy (using sockets), I get :

Client                                    Server
  | ---- TCP Payloads from packet 1 + 2 ---> |
  | <--- TCP Payloads from packet 3 + 4 ---- |

My problem is that the sockets are putting together multiple TCP payloads together. I would like to avoid this behavior.

I could circumvent the problem using the size of the packets, but this size is not constant. I tried using the tcpNoDelay option, but also no luck with that. I used the networking framework netty, but I get the same problem.

Is there a way to avoid this concatenation of TCP payloads in Java ?


No. TCP is a stream-oriented protocol - this is how it should work. If you want to see the fragmented packets introduced by the various hops in between you and the peer, you'll need a packet capture library.

You could just as well receive the packets 1 byte at a time, the concept of "chunks" of data is gone as soon as the client delivers its payload to the IP stack. Using TCP_NODELAY simply ensures that the sender will transmit data immediately - not that all hops up to and including the recipient will avoid combining packets.

0

精彩评论

暂无评论...
验证码 换一张
取 消