首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置TCP_NODELAY时,设置TCP_CORK的行为如何?

问设置TCP_NODELAY时,设置TCP_CORK的行为如何?
EN

Stack Overflow用户
提问于 2021-01-25 14:04:48
回答 1查看 380关注 0票数 0

根据手册,TCP_CORK阻止发送部分,取消设置它刷新部分。拥有TCP_CORK设置和设置TCP_NODELAY也会刷新部分数据。听起来很相似,到底有什么区别呢?

在具有TCP_CORK集的连接上:

  1. 说的对吗? TCP_NODELAY最多只刷新一个部分数据包,然后返回到TCP_CORK行为中吗?(所以这更像是一种事件,而不是一种永久的选择!)(手册页显式地提到了All queued partial frames are sent when the option is cleared again.,怎么会有多个排队的部分帧?不把所有的部分合并成一个包吗?)
  2. 可以在TCP_CORK连接上反复设置TCP_NODELAY以清除挂起的数据?(或者需要先取消设置?)

man tcp

代码语言:javascript
复制
   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.
EN

回答 1

Stack Overflow用户

发布于 2021-01-25 16:55:15

这不是一个真正的答案,但我想我可以分享我得到的一切。

我做了个小测试。

客户端:

代码语言:javascript
复制
    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);

服务器:

代码语言:javascript
复制
# 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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65886303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档