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.
相关推荐
Xzq2105097 小时前
IP协议——网络层协议
服务器·网络·tcp/ip
铭哥的编程日记8 小时前
DNS解析 HTTP TCP/IP ICMP/NAT/NAPT相关知识点
网络协议·tcp/ip·http
人工干智能8 小时前
科普:OpenClaw、大模型、通道及云端养虾
网络·人工智能·llm
小江的记录本8 小时前
【AOP】AOP-面向切面编程 (系统性知识体系全解)
java·前端·后端·python·网络协议·青少年编程·代理模式
安逸sgr8 小时前
MCP 协议深度解析(一):MCP 协议概览与架构设计
服务器·网络·人工智能·网络协议·agent·mcp
卢锡荣8 小时前
LDR6500|超小封装 Type‑C DRP PD 控制芯片:边充边传,一芯极简,全能适配
开发语言·网络·人工智能·计算机外设·电脑
头疼的程序员8 小时前
计算机网络:自顶向下方法(第七版)第四章 学习分享(一)
网络·学习·计算机网络
凉、介8 小时前
深入 LWIP:数据是如何被封装并发送出去的
网络·驱动开发·笔记·学习·嵌入式
不知名。。。。。。。。9 小时前
仿muduo库实现高并发服务器----通信链接管理Connection
运维·服务器·网络
MaximusCoder18 小时前
等保测评命令——Anolis Linux
linux·运维·服务器·网络·经验分享·安全·php