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.
相关推荐
说不得明天6 小时前
网络管理:AutoarNM部分
c语言·网络·mcu·汽车·autosar
xhbh6666 小时前
无公网IP环境下的宽带端口映射:80km穿云箭部署与性能测试
网络·智能路由器
胡志辉的博客6 小时前
邮件中点击“加载图片”,你的IP地址已经被泄漏
网络协议·user-agent·加载图片 ip 泄漏·邮件远程图片·追踪像素·邮件隐私保护·tracking pixel
lularible6 小时前
PTP协议精讲(4.4):从时钟程序实现——时间的“追随者“
网络·网络协议·开源·嵌入式·ptp
小辰记事本7 小时前
RDMA:AI算力集群的“网络命脉”
网络·人工智能·网络协议·rdma
缪懿7 小时前
javaEE:网络编程基础
java·网络·java-ee
BizViewStudio7 小时前
2026 年网站建设行业白皮书:AI 深度融合与合规驱动下的 6 大变革方向——附优质开发商
大数据·网络·人工智能·microsoft·媒体
切糕师学AI7 小时前
深入解析 gRPC:高性能开源 RPC 框架的原理与实战
网络协议·rpc·开源·grpc
500佰7 小时前
我唯一的一个变现产品,说说它的逻辑
网络·职场和发展·idea·个人开发·软件需求
浪客灿心7 小时前
Linux网络NAT
linux·网络