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
相关文章
|
3天前
|
iOS开发 MacOS Windows
|
3天前
|
监控 安全 Linux
【专栏】Linux中六个常用的网络命令:ping、traceroute、netstat、nmap、ifconfig和ip
【4月更文挑战第28天】本文介绍了Linux中六个常用的网络命令:ping、traceroute、netstat、nmap、ifconfig和ip,以及它们在测试网络连通性、追踪路由、查看网络状态、安全扫描和接口配置等场景的应用。通过学习和运用这些命令,系统管理员和网络爱好者能更有效地诊断和管理网络问题,确保网络稳定运行。
|
1天前
|
运维 网络协议 Linux
Docker网络_docker 网络,来看看这份超全面的《Linux运维面试题及解析》
Docker网络_docker 网络,来看看这份超全面的《Linux运维面试题及解析》
|
3天前
|
网络协议 Linux 网络架构
|
3天前
|
域名解析 网络协议 Linux
linux网络配置详解
linux网络配置详解
22 0
|
3天前
|
网络协议 Java Linux
【探索Linux】P.29(网络编程套接字 —— 简单的TCP网络程序模拟实现)
【探索Linux】P.29(网络编程套接字 —— 简单的TCP网络程序模拟实现)
14 0
|
3天前
|
存储 网络协议 算法
【探索Linux】P.28(网络编程套接字 —— 简单的UDP网络程序模拟实现)
【探索Linux】P.28(网络编程套接字 —— 简单的UDP网络程序模拟实现)
16 0
|
3天前
|
网络协议 算法 Linux
【探索Linux】P.27(网络编程套接字 —— UDP协议介绍 | TCP协议介绍 | UDP 和 TCP 的异同)
【探索Linux】P.27(网络编程套接字 —— UDP协议介绍 | TCP协议介绍 | UDP 和 TCP 的异同)
16 0
|
3天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
13 0