TCP netstat TIME_WAIT & CLOSE_WAIT

TIME_WAIT is different from CLOSE_WAIT, and it's not necessarily a client-side fault. It happens due to how TCP handles connection termination.

Key Differences

TCP State Cause Who is responsible? Fix/Optimization
CLOSE_WAIT Server didn't close() the socket after client closed it. Server-side issue (socket left open). Ensure close(client_fd); is always called.
TIME_WAIT Server closed the socket first, waiting to ensure the client received the FIN. Normal behavior (not necessarily a problem). Use SO_LINGER or SO_REUSEADDR to mitigate excessive TIME_WAIT.

Why Does TIME_WAIT Happen?

  • When your server calls close(client_fd); , it sends a FIN to the client.
  • If the client follows the normal TCP closure process , it replies with ACK and sends its own FIN, which your server then ACKs.
  • Your server enters TIME_WAIT for 2×MSL (Maximum Segment Lifetime, usually 60s total) to ensure any delayed packets are handled before fully releasing the connection.

Is TIME_WAIT the Client's Fault?

  • If the client closes the connection first , the server goes to CLOSE_WAIT (which we fixed).
  • If the server closes first , TIME_WAIT is expected.
  • Too many TIME_WAIT sockets? It usually means many short-lived connections are being closed quickly.

How to Reduce TIME_WAIT Issues?

  1. Enable SO_REUSEADDR

    • Allows immediate reuse of the same port, even in TIME_WAIT state.
    c 复制代码
    int opt = 1;
    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  2. Use SO_LINGER to Force Immediate Closure

    • Not recommended unless necessary, as it can cause packet loss.
    c 复制代码
    struct linger sl;
    sl.l_onoff = 1;  // Enable linger
    sl.l_linger = 2; // Close socket after 2 seconds
    setsockopt(client_fd, SOL_SOCKET, SO_LINGER, &sl, sizeof(sl));
  3. Keep Connections Open Longer

    • If possible, reuse connections instead of opening/closing frequently.
  4. Tune TCP Stack (sysctl)

    • Reduce TIME_WAIT timeout if necessary:
    sh 复制代码
    sudo sysctl -p | grep net.ipv4.tcp_fin_timeout  # net.ipv4.tcp_fin_timeout = 2
    sysctl -w net.ipv4.tcp_fin_timeout=30

Conclusion

  • CLOSE_WAIT is a server-side issue (fixed by properly closing sockets).
  • TIME_WAIT is expected behavior when the server closes first.
  • Too many TIME_WAIT sockets? Use SO_REUSEADDR , linger options , or connection pooling if appropriate.
相关推荐
Xu_20062 分钟前
IAP/OTA实战1--bootloader程序:OTA程序(OTA.c/OTA.h)
网络
深圳光特通信豆子1 小时前
从物料到工艺:深度拆解千兆SFP单模光模块(GTFP-GE-LX)的硬核制造逻辑
网络
Chengbei111 小时前
ARTEX-Pro衍生版、优化漏洞报告、定时启停夜间半价跑任务、高危发现二次复核减少误报、ZAP MCP 与预装工具全面升级
网络·人工智能·安全·web安全·网络安全
鲁邦通物联网1 小时前
机器人5G工业路由器量产验证:业务指标与配置基线
网络·5g·机器人·工业路由器·4g路由器·工业级路由器·5g工业路由器
YIAN4 小时前
面试串讲:OSI 七层、TCP 三次握手与 LangChain Agent 核心组件
tcp/ip·面试·langchain
企业解惑小能手-域智盾系统4 小时前
加密文档被成功外发?一次从“失效”倒推的排查复盘
网络·安全
K成长日志5 小时前
BLE Host层GAP -- 广播、发现、连接与绑定
网络·物联网·无线通信·蓝牙·iot·ble
leijiwen6 小时前
IP 智能:链生之境让IP从品牌形象到品牌智能
网络·网络协议·tcp/ip
楚识科技6 小时前
昇腾 / 海光环境跑档案票据 OCR:算子适配、私有化部署与性能验证
网络·人工智能·ocr