<span id='acwfb'></span>
        <acronym id='acwfb'><em id='acwfb'></em><td id='acwfb'><div id='acwfb'></div></td></acronym><address id='acwfb'><big id='acwfb'><big id='acwfb'></big><legend id='acwfb'></legend></big></address>

      1. <i id='acwfb'></i>

      2. <ins id='acwfb'></ins>
      3. <fieldset id='acwfb'></fieldset>

        1. <tr id='acwfb'><strong id='acwfb'></strong><small id='acwfb'></small><button id='acwfb'></button><li id='acwfb'><noscript id='acwfb'><big id='acwfb'></big><dt id='acwfb'></dt></noscript></li></tr><ol id='acwfb'><table id='acwfb'><blockquote id='acwfb'><tbody id='acwfb'></tbody></blockquote></table></ol><u id='acwfb'></u><kbd id='acwfb'><kbd id='acwfb'></kbd></kbd>
        2. <i id='acwfb'><div id='acwfb'><ins id='acwfb'></ins></div></i>

          <code id='acwfb'><strong id='acwfb'></strong></code>
          <dl id='acwfb'></dl>

          Linux操作系统下TCP keepalive属性查看

          • 时间:
          • 浏览:31
          • 来源:124软件资讯网

            Tcp是面向毗连的,在现实应用中通常都需要检测毗连是否还可用.若是不行用,可分为:

            a. 毗连的对规矩常关闭.

            b. 毗连的对端非正常关闭,这包罗对端装备掉电,法式瓦解,网络被中止等.这种情形是不能也无法通知对端的,以是毗连会一直存在,铺张国家的资源.

            tcp协议栈有个keepalive的属性,可以自动探测socket是否可用,不外这个属性的默认值很大.

            全局设置可更改/etc/sysctl.conf,加上:

            net.ipv4.tcp_keepalive_intvl = 20
            net.ipv4.tcp_keepalive_probes = 3
            net.ipv4.tcp_keepalive_time = 60

            在法式中设置如下:

            #include
            #include
            #include
            #include
            #include
            int keepAlive = 1; // 开启keepalive属性
            int keepIdle = 60; // 如该毗连在60秒内没有任何数据往来,则举行探测
            int keepInterval = 5; // 探测时发包的时间距离为5 秒
            int keepCount = 3; // 探测实验的次数.若是第1次探测包就收到响应了,则后2次的不再发.

            setsockopt(rs, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
            setsockopt(rs, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle));
            setsockopt(rs, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));
            setsockopt(rs, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));

            在法式中体现为,当tcp检测到对端socket不再可用时(不能发出探测包,或探测包没有收到ACK的响应包),select会返回socket可读,而且在recv时返回-1,同时置上errno为ETIMEDOUT  。