linux的网络连接状态

简介:

一、TCP连接的几种状态:

下图为状态表,

wKiom1ghoE-jz5s1AAHAS8PedVs202.png-wh_50

TCP protocol operations may be divided into three phases. Connections must be properly established in a multi-step handshake process (connection establishmentbefore entering the data transfer phase. After data transmission is completed, the connection termination closes established virtual circuits and releases all allocated resources.

A TCP connection is managed by an operating system through a programming interface that represents the local end-point for communications, the Internet socket. During the lifetime of a TCP connection the local end-point undergoes a series of state changes.

操作系统通过代表通信本地端的一个叫网络socket的一个可编程接口来管理TCP连接。在TCP连接周期中,本地端会经历一系列状态变化。


  • LISTEN

  • (server) represents waiting for a connection request from any remote TCP and port.

  • SYN-SENT

  • (client) represents waiting for a matching connection request after having sent a connection request.

  • SYN-RECEIVED

  • (server) represents waiting for a confirming connection request acknowledgment after having both received and sent a connection request.

  • ESTABLISHED

  • (both server and client) represents an open connection, data received can be delivered to the user. The normal state for the data transfer phase of the connection.

  • FIN-WAIT-1

  • (both server and client) represents waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent.

  • FIN-WAIT-2

  • (both server and client) represents waiting for a connection termination request from the remote TCP.

  • CLOSE-WAIT

  • (both server and client) represents waiting for a connection termination request from the local user.

  • CLOSING

  • (both server and client) represents waiting for a connection termination request acknowledgment from the remote TCP.

  • LAST-ACK

  • (both server and client) represents waiting for an acknowledgment of the connection termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request).

  • TIME-WAIT

  • (either server or client) represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. a connection can stay in TIME-WAIT for a maximum of four minutes known as two MSL (maximum segment lifetime).]

  • CLOSED

  • (both server and client) represents no connection state at all.


二、怎么样建立连接(3次握手):

  1. SYN: The active open is performed by the client sending a SYN to the server. The client sets the segment's sequence number to a random value A.

  2. SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the received sequence number i.e. A+1, and the sequence number that the server chooses for the packet is another random number, B.

  3. ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement value i.e. A+1, and the acknowledgement number is set to one more than the received sequence number i.e. B+1.

At this point, both the client and server have received an acknowledgment of the connection. The steps 1, 2 establish the connection parameter (sequence number) for one direction and it is acknowledged. The steps 2, 3 establish the connection parameter (sequence number) for the other direction and it is acknowledged. With these, a full-duplex communication is established.



三、连接终止(4次挥手):

wKioL1ghoMHAJgC4AAAnTEOUqFA284.png-wh_50

The connection termination phase uses a four-way handshake(4次挥手), with each side of the connection terminating independently. When an endpoint wishes to stop its half of the connection, it transmits a FIN packet, which the other end acknowledges(告知已收到) with an ACK. Therefore, a typical tear-down requires a pair of FIN and ACK segments from each TCP endpoint. After the side that sent the first FIN has responded with the final ACK, it waits for a timeout before finally closing the connection, during which time the local port is unavailable for new connections; this prevents confusion due to delayed packets being delivered during subsequent connections.

A connection can be "half-open", in which case one side has terminated its end, but the other has not. The side that has terminated can no longer send any data into the connection, but the other side can. The terminating side should continue reading the data until the other side terminates as well.

It is also possible to terminate the connection by a 3-way handshake, when host A sends a FIN and host B replies with a FIN & ACK (merely combines 2 steps into one) and host A replies with an ACK.

Some host TCP stacks may implement a half-duplex close sequence, as Linux or HP-UX do. If such a host actively closes a connection but still has not read all the incoming data the stack already received from the link, this host sends a RST instead of a FIN . This allows a TCP application to be sure the remote application has read all the data the former sent—waiting the FIN from the remote side, when it actively closes the connection. But the remote TCP stack cannot distinguish between a Connection Aborting RST and Data Loss RST. Both cause the remote stack to lose all the data received.

Some application protocols may violate the OSI model layers, using the TCP open/close handshaking for the application protocol open/close handshaking — these may find the RST problem on active close. As an example:

s = connect(remote);
send(s, data);
close(s);

For a usual program flow like above, a TCP/IP stack like that described above does not guarantee that all the data arrives to the other application.


四、中文解释3次握手,4次挥手

http://www.52im.net/thread-258-1-1.html(网上找的一篇文章)


五、为什么TIME_WAIT状态还需要等待2MSL才能回到CLOSED状态?或者为什么TCP要引入TIME_WAIT状态?

当TCP执行一个主动关闭,并发回最后一个ACK后,该连接必须在TIME_WAIT状态停留的时间为2倍的MSL,这样可以让TCP再次发送最后的ACK以防止这个ACK丢失(另一端超时重发最后的FIN)。MSL(Maximum Segment Lifetime)即最大生存时间,常用值为30秒、1分钟或者2分钟。















本文转自chenzudao51CTO博客,原文链接:http://blog.51cto.com/victor2016/1870766 ,如需转载请自行联系原作者




相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
6天前
|
监控 安全 Linux
【专栏】Linux 中有哪些常用的网络命令,分别适用于什么场景?
【4月更文挑战第28天】本文介绍了Linux中六个常用的网络命令:ping、traceroute、netstat、nmap、ifconfig和ip,以及它们在测试网络连通性、追踪路由、查看网络状态、安全扫描和接口配置等场景的应用。通过学习和运用这些命令,系统管理员和网络爱好者能更有效地诊断和管理网络问题,确保网络稳定运行。
|
7天前
|
网络协议 算法 Linux
【Linux】深入探索:Linux网络调试、追踪与优化
【Linux】深入探索:Linux网络调试、追踪与优化
|
4天前
|
Ubuntu 网络协议 Linux
|
4天前
|
JSON 网络协议 Linux
Linux ip命令:网络的瑞士军刀
【4月更文挑战第25天】
9 1
|
5天前
|
缓存 网络协议 Linux
Linux 网络命令大全,详细归纳!
【4月更文挑战第24天】
30 3
Linux 网络命令大全,详细归纳!
|
6天前
|
网络协议 JavaScript Linux
Linux常用网络指令(下)
Linux常用网络指令(下)
14 0
|
6天前
|
Linux
Linux常用网络指令(上)
Linux常用网络指令(上)
7 0
|
6天前
|
安全 Linux 网络安全
【专栏】Linux 网络扫描工具:nmap,涨知识的时间到了!
【4月更文挑战第28天】nmap, 开源网络扫描工具,用于探测主机、网络信息,包括开放端口、服务类型、OS等。本文分三部分介绍:1) nmap简介与基本原理;2) 使用方法和高级技巧,如脚本扩展;3) 实际应用,如网络安全评估、系统管理和渗透测试。学习nmap需注意合规性,持续探索新技巧,以提升网络管理与安全能力。一起开始nmap的探索之旅吧!
|
6天前
|
安全 网络协议 Linux
【专栏】一文教你玩转 Linux 的 ping 命令,从此成为 Linux 网络高手
【4月更文挑战第28天】本文详细介绍了Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法。通过ping,用户可测试网络连通性、诊断故障及评估性能。此外,文章还讨论了ping在不同协议、模拟网络环境及与其他命令结合使用时的场景。注意防火墙和网络环境可能影响ping结果,理解错误信息有助于网络问题排查。熟练掌握ping命令,能助你成为Linux网络专家。不断学习和实践,提升网络技能,为构建稳定网络环境贡献力量。
|
6天前
|
运维 监控 安全
【专栏】运维工程师必须知道的 11 个 Linux 网络命令
【4月更文挑战第28天】本文介绍了11个必备的Linux网络命令,包括ifconfig、ip、ping、traceroute、netstat、tcpdump、ss、telnet、ftp、ssh和nmap,这些命令在网络配置、故障排查和性能监控中发挥关键作用。通过实例分析,强调了它们在实际工作中的综合应用,帮助运维工程师提升效率和应对网络挑战。学习和熟练运用这些命令是提升网络管理能力的基础。