gpt4 book ai didi

boost-asio - 无法关闭 TCP_NODELAY

转载 作者:行者123 更新时间:2023-12-05 09:24:13 45 4
gpt4 key购买 nike

我正在使用 Boost asio 发送 TCP 消息。我设置了 NO_DELAY 选项,因为这是一个“实时”控制系统。我看到使用 Wireshark 在消息中设置了 PSH 标志。我对性能感到满意,并且按预期工作。

出于兴趣,我决定关闭 NO_DELAY 并测量性能差异。

我交换了我现有的代码:

m_tcpSocket.open(boost::asio::ip::tcp::v4());
boost::asio::ip::tcp::no_delay noDelayOption(true);
m_tcpSocket.set_option(noDelayOption);

// snip create endpoint
m_tcpSocket.connect(m_tcpServerEndpoint);

// snip build message
m_tcpSocket.send(boost::asio::buffer(pDataBuffer, size));

对于

boost::asio::ip::tcp::no_delay noDelayOption(false);
m_tcpSocket.set_option(noDelayOption);

我仍然看到 PSH 标志已设置。

我也尝试删除 set_option 代码,但仍然看到它已设置。

在 Wireshark 中我看到:

104 - 105  SYN
105 - 104 SYN, ACK
104 - 105 ACK
104 - 105 PSH, ACK + my message
105 - 104 ACK

其中 104 和 105 是我的两台 PC 的 IP 地址。我也很惊讶带有我的数据的消息有一个 ACK​​。

如何关闭 NO_DELAY?

最佳答案

您的代码看起来好像正确地设置了 TCP_NODELAY 打开或关闭。要关闭 TCP_NODELAY,请使用:

socket.set_option(boost::asio::ip::tcp::no_delay(false));

TCP RFC将 PSH 定义为推送函数。简而言之,它是一个标志,通知接收方所有数据已发送完毕,因此将数据向上转发到协议(protocol)栈。 Boost.Asio maps它的 API 到 BSD 套接字,而 BSD 套接字不提供控制 PSH 标志的方法。这通常由协议(protocol)栈中的内核在清除其缓冲区时处理。

来自 TCP/IP Illustrated :

This flag is conventionally used to indicate that the buffer at the side sending the packet has been emptied in conjunction with sending the packet. In other words, when the packet with the PSH bit field set left the sender, the sender had no more data to send.

[...]

Push (the receiver should pass this data to the application as soon as possible—not reliably implemented or used).

关于boost-asio - 无法关闭 TCP_NODELAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15294816/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com