动态更改套接字选项

时间:2015-12-05 00:33:25

标签: linux sockets tcp posix

我希望我能在单个插座的整个生命周期内快速,多次来回更改setsockopt的视频。

具体来说,我需要启用和禁用TCP_NODELAY,具体取决于某些业务逻辑。有些数据包需要与TCP_NODELAY一起发送,而有些数据包是批量发送的,如果没有数据包就应该出去。我们提前知道,所以我想设置合适的选项。这是一个很长的故事 - 这个问题关于启用或禁用Nagle的优点或缺点!

setsockopt通话生效前是否有延迟?

1 个答案:

答案 0 :(得分:2)

引用源,它应该没有延迟,立即在tcp_sk上设置选项:

do_tcp_setsockopt

...
case TCP_NODELAY:
    if (val) {
        /* TCP_NODELAY is weaker than TCP_CORK, so that
         * this option on corked socket is remembered, but
         * it is not activated until cork is cleared.
         *
         * However, when TCP_NODELAY is set we make
         * an explicit push, which overrides even TCP_CORK
         * for currently queued segments.
         */
        tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
        tcp_push_pending_frames(sk);
    } else {
        tp->nonagle &= ~TCP_NAGLE_OFF;
    }
    break;

__tcp_push_pending_frames

tcp_write_xmit