在单独的 TCP 段中发送数据而不被 TCP 堆栈合并 [英] Send data in separate TCP segments without being merged by the TCP stack

查看:18
本文介绍了在单独的 TCP 段中发送数据而不被 TCP 堆栈合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux(内核 3.0.0.12;带有 GCC 4.6.1 的 C++)上,我想通过 (POSIX) 发送调用彼此发送几个 TCP 数据包.

On Linux (Kernel 3.0.0.12; C++ with GCC 4.6.1) I want to send shortly after each other a few TCP-packets via (POSIX) send-call.

使用 Wireshark,我可以看到数据包不是在单独的 TCP 帧中发送的,而是两个或更多在一个数据包中一起发送.

With Wireshark I can see that the packets weren't sent in separate TCP-frames, but two or more were sent together in one packet.

有没有办法告诉系统从自己的 TCP 数据包中的一个 send() 缓冲区发送数据?程序流不应在发送调用时阻塞.

Is there any way to tell the system to send data from one send()-buffer in own TCP-packet? The programmflow shouldn't block at the send-call.

推荐答案

您的 TCP 堆栈正在实现 Nagle 的算法,试图通过缓冲数据来提高效率.它是一种常见的优化,其目的是分摊 40+ 字节(TCP + IP)标头的成本.堆栈不是发送多个小数据包,而是缓冲数据并将它们组合成一个更大的数据包,从而减少标头开销.

Your TCP stack is implementing Nagle's algorithm trying to increase efficiency by buffering data. Its a common optimization where the intent is amortize the cost of the 40+ byte (TCP + IP) header. Instead of sending multiple small packets, the stack buffers the data and combines them into a larger packet thereby reducing the header overhead.

不过,TCP 堆栈不会无限期地缓冲.只要连接上有一些未确认的发送数据,发送方就会继续缓冲.下一个数据包在以下时间发出:

TCP stacks don't buffer indefinitely though. As long as there is some un-acknowledged sent data on the connection, the sender continues to buffer. The next packet is sent out when:

  • 可以发送完整的数据包
  • 接收方确认所有之前发送的数据

您通常可以通过设置 TCP_NODELAY 套接字选项来禁用此功能.

You can usually disable this by setting the TCP_NODELAY socket option.

setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));

这篇关于在单独的 TCP 段中发送数据而不被 TCP 堆栈合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆