V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
thomaswang
V2EX  ›  程序员

unix 设置保活机制

  •  
  •   thomaswang · Jan 18, 2018 · 2195 views
    This topic created in 3022 days ago, the information mentioned may be changed or developed.
    int keepAlive = 1;   // 开启 keepalive 属性. 缺省值: 0(关闭)  
    int keepIdle = 60;   // 如果在 60 秒内没有任何数据交互,则进行探测. 缺省值:7200(s)  
    int keepInterval = 5;   // 探测时发探测包的时间间隔为 5 秒. 缺省值:75(s)  
    int keepCount = 2;   // 探测重试的次数. 全部超时则认定连接失效..缺省值:9(次)  
    setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void*)&keepAlive, sizeof(keepAlive));  
    setsockopt(s, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle));  
    setsockopt(s, SOL_TCP, TCP_KEEPINTVL, (void*)&keepInterval, sizeof(keepInterval));  
    setsockopt(s, SOL_TCP, TCP_KEEPCNT, (void*)&keepCount, sizeof(keepCount));  
    

    这个是 server 端还是 client 端设置,还是都设置?

    4 replies    2018-01-20 10:14:55 +08:00
    Panic
        2
    Panic  
       Jan 19, 2018
    保活一般都是应用自己加的,基本没人用 tcp 层面的 KEEPALIVE
    thomaswang
        3
    thomaswang  
    OP
       Jan 19, 2018
    @Panic 能说说自己怎么加吗,服务端(对每个连接)、客户端都要设置计时,到时间就发个包给对放 ?
    Panic
        4
    Panic  
       Jan 20, 2018
    @thomaswang 不叫保活, 不都叫心跳吗, 反正我们自己都是用心跳的. 类似下面这种
    https://segmentfault.com/a/1190000006832547
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   991 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 19:30 · PVG 03:30 · LAX 12:30 · JFK 15:30
    ♥ Do have faith in what you're doing.