根据手册,TCP_CORK阻止发送部分,取消设置它刷新部分。拥有TCP_CORK设置和设置TCP_NODELAY也会刷新部分数据。听起来很相似,到底有什么区别呢?
在具有TCP_CORK集的连接上:
All queued partial frames are sent when the option is cleared again.,怎么会有多个排队的部分帧?不把所有的部分合并成一个包吗?)man tcp
TCP_CORK (since Linux 2.2)
If set, don't send out partial frames. All queued partial frames are sent when the option is cleared again. This is useful for prepending headers before calling sendfile(2), or for
throughput optimization. As currently implemented, there is a 200 millisecond ceiling on the time for which output is corked by TCP_CORK. If this ceiling is reached, then queued
data is automatically transmitted. This option can be combined with TCP_NODELAY only since Linux 2.5.71. This option should not be used in code intended to be portable.
TCP_NODELAY
If set, disable the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data. When not set, data is buffered until
there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets, which results in poor utilization of the network. This option is overridden by
TCP_CORK; however, setting this option forces an explicit flush of pending output, even if TCP_CORK is currently set.发布于 2021-01-25 16:55:15
这不是一个真正的答案,但我想我可以分享我得到的一切。
我做了个小测试。
客户端:
optval = 1;
setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(int));
write(sockfd, "foo\n", 4);
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int));
usleep(70000);
write(sockfd, "bar\n", 4);
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int));
usleep(70000);
write(sockfd, "hello\n", 6);
usleep(70000);
write(sockfd, "world\n", 6);服务器:
# netcat -l 8123 | ts -i "%.S"
17.365721 foo
00.070271 bar
00.140462 hello
00.000216 world选择的70 of延迟低于TCP_CORK标志的200 of超时。
如您所见,可以使用TCP_NODELAY重复刷新数据。(问题2已获答覆)
奖励:当查看线路上的数据包时,我们可以看到在设置TCP_NODELAY时,在数据包上设置了PSH标志。因此,数据也可能在接收端被刷新。(比较Difference between push and urgent flags in TCP)
https://stackoverflow.com/questions/65886303
复制相似问题