问题
I'm using Boost asio to send a TCP message. I set the NO_DELAY option because this is a 'real time' control system. I see the PSH flag set in the message using Wireshark. I am happy with the performance and it is working as expected.
For interest, I decided to turn the NO_DELAY off and measure the performance difference.
I swapped my existing code:
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));
for
boost::asio::ip::tcp::no_delay noDelayOption(false);
m_tcpSocket.set_option(noDelayOption);
and I still see the PSH flag set.
I also tried removing the set_option code and still see it set.
In Wireshark I see:
104 - 105 SYN
105 - 104 SYN, ACK
104 - 105 ACK
104 - 105 PSH, ACK + my message
105 - 104 ACK
where 104 and 105 are IP addresses of my 2 PCs. I am also surprised that the message with my data has an ACK.
How do I turn NO_DELAY off?
回答1:
Your code looks as though it is properly setting TCP_NODELAY
on or off. To set TCP_NODELAY
off, use:
socket.set_option(boost::asio::ip::tcp::no_delay(false));
The TCP RFC defines PSH as the push function. In short, it is a flag that informs the receiver that all data has been sent, so forward data up the protocol stack. Boost.Asio maps its API to BSD sockets, and BSD sockets do not provide a way to control the PSH flag. This is often handled by the kernel within the protocol stack, when it clears its buffer.
From 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).
回答2:
NO_DELAY does not change flags that is sent to peer. It changes kernel's buffering algorithm. Google about Nagle algorithm to get more understanding about it.
来源:https://stackoverflow.com/questions/15294816/cant-turn-tcp-nodelay-off