TCP netstat TIME_WAIT CLOSE_WAIT

news/2025/3/26 22:03:03/

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.

http://www.ppmy.cn/news/1582943.html

相关文章

Cocos Creator Shader入门实战(六):使用setProperty动态设置材质属性,以及材质常用接口

引擎:3.8.5 您好,我是鹤九日! 回顾 上篇文章,我们主要讲解了关于材质的使用,主要有这么几点: 一、没有Effect资源,材质无从说起。 二、材质的构建,支持编译器和代码的动态构建 三…

35-长度最小的子数组

给定一个含有 n 个正整数的数组和一个正整数 target 。 找出该数组中满足其总和大于等于 target 的长度最小的 子数组 [numsl, numsl1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。 方法一:暴力枚举法 通过…

【Java面试系列】初识GateWay网关

网关介绍 在微服务架构中,一个系统会被拆分为很多个微服务,那么作为客户端要如何调用这么多微服务呢?如果没有网关的存在,我们只能在客户端记录每个微服务的地址,然后分别去调用。这样的话会产生很多问题。例如&#…

剑指 Offer II 117. 相似的字符串

comments: true edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20117.%20%E7%9B%B8%E4%BC%BC%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/README.md 剑指 Offer II 117. 相似的字符串 题目描述 如果交换字符串 X 中的两个不同位置…

android Kotlin原理

目录 一,概述 1.1 kotlin协程序原理: 1.2 核心概念 二,协程调度器之Dispatchers 三,协程能进行线程恢复的原理 一,概述 1.1 kotlin协程序原理: 1,内部线程池管理线程使用到了自旋和挂起 2,传统的线程之所以重,是因为线程的执行,等待唤醒需要操作系统来完成 …

.gitignore使用指南

.gitignore使用指南 目录 什么是.gitignore为什么需要.gitignore如何创建.gitignore文件.gitignore文件的语法规则 忽略单个文件忽略目录忽略特定类型的文件不忽略特定文件或目录递归匹配 示例.gitignore文件注意事项更多特殊场景匹配规则 忽略多个特定后缀的文件忽略特定目录…

PHP If...Else 语句详解

PHP If...Else 语句详解 引言 PHP 是一种流行的服务器端脚本语言,常用于开发动态网站和应用程序。在 PHP 编程中,条件语句是编程逻辑的基础,其中 if...else 语句是最基本且最常用的条件语句之一。本文将详细介绍 PHP 的 if...else 语句&…

Spring6: 2 入门

2、 入门 2.1 环境要求 JDK:Java17(Spring6要求JDK最低版本是Java17) Maven:3.6 Spring:6.0.2 2.2、构建模块 1)构建父模块spring6 在idea中,依次单击 File -> New -> Project -&g…