当前位置: 首页 > news >正文

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 StateCauseWho is responsible?Fix/Optimization
CLOSE_WAITServer didn’t close() the socket after client closed it.Server-side issue (socket left open).Ensure close(client_fd); is always called.
TIME_WAITServer 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.
    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.
    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:
    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.

相关文章:

  • 利用dify打造命令行助手
  • 基于SpringBoot + Vue 的垃圾分类管理系统
  • Qt信号与槽机制入门详解:从基础语法到界面交互实战
  • 测试用例组成及设计方法
  • Zotero·Awesome GPT配置
  • 基于CentOS系统搭建Samba服务
  • 提高库存周转率的重要性
  • 风格混合增强的解纠缠学习在医学图像分割的无监督域自适应中的应用|文献速递-医学影像人工智能进展
  • 【USTC 计算机网络】第三章:传输层 - 可靠数据传输的原理
  • MAC terminal
  • 2025-3-23 leetcode刷题情况(动态规划)
  • 不能解析域名怎么回事?
  • 游戏引擎学习第180天
  • SQL中体会多对多
  • C++11中智能指针的使用(shared_ptr、unique_ptr、weak_ptr)
  • FPGA_YOLO(三)
  • Python使用SVC算法解决乳腺癌数据集分类问题——寻找最佳核函数
  • 【UEFI】关于Secure Boot
  • 2.3.5 覆盖率数据的合并
  • 【前端】使用 HTML、CSS 和 JavaScript 创建一个数字时钟和搜索功能的网页
  • 孙一凡的东欧狂想音乐会:一场穿越东欧的听觉绮梦
  • 山东一景区怕游客赶不到海撒三千斤蛤蜊:给游客提供情绪价值
  • AI世界的年轻人|他用影像大模型解决看病难题,“要做的研究还有很多”
  • 宿州市委副书记任东已任市政府党组书记
  • 增诉滥用职权罪,尹锡悦遭韩国检方追加起诉
  • 内蒙古公开宣判144件毁林毁草刑案,单起非法占用林地逾250亩