Java套接字缓冲区问题
我目前正在用 Java 构建一个透明代理。这个透明代理放置在客户端和服务器之间,使用 iptables 重定向 TCP 流。
从 TCP 通信的角度来看,我有以下对话框:
Client Server | ---- TCP Packet 1 ---> | | ---- TCP Packet 2 ---> | | <--- TCP Packet 3 ---- | | <--- TCP Packet 4 ---- |
从透明代理(使用套接字)的角度来看,我得到:
Client Server | ---- TCP Payloads from packet 1 + 2 ---> | | <--- TCP Payloads from packet 3 + 4 ---- |
我的问题是套接字将多个 TCP 有效负载放在一起。我想避免这种行为。
我可以使用数据包的大小来规避这个问题,但这个大小不是恒定的。我尝试使用 tcpNoDelay 选项,但也没有成功。我使用了网络框架netty,但我遇到了同样的问题。
有没有办法避免 Java 中 TCP 有效负载的这种串联?
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 point 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。TCP 是一种面向流的协议 - 这就是它应该的工作方式。如果您想查看您和对等方之间的各个跃点引入的碎片数据包,您将需要一个数据包捕获库。
您也可以一次接收 1 个字节的数据包,一旦客户端将其有效负载传送到 IP 堆栈,数据“块”的概念就消失了。使用 TCP_NODELAY 只是确保发送方立即传输数据 - 并不是所有到达接收方(包括接收方)的跳跃都会避免合并数据包。
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.