[Turn] TCP three-way handshake with the four waving understand and face questions

Transfer: https://blog.csdn.net/qq_38950316/article/details/81087809

 SEQ ID NO seq: 4 bytes for the data segment order mark, the TCP connection all data bytes transmitted are compiled on a serial number, the first byte of the random number generated by the local; ed to bytes after the serial number, give each segment is assigned a serial number; seq is the sequence number of the first byte of the data segment number.

    Acknowledgment number ack: 4 bytes, the next expects to receive the other of the first data byte of a segment number; first byte of the segment sequence number indicates the number of data carrying; numbers refer confirmed It is expected to be received next byte number; therefore this number plus the last byte of the segment is the acknowledgment number.

    Acknowledgment ACK: one bit, only when ACK = 1, the acknowledgment number field is valid. When ACK = 0, the acknowledgment number is invalid

    Synchronization SYN: for synchronizing serial connection establishment. Means that when SYN = 1, ACK = 0: this is a connection request packet segments. If the agreed connection, in the response message segments that SYN = 1, ACK = 1. Accordingly, SYN = 1 indicates that this is a connection request, or the connection acceptance message. The SYN flag only if the TCP connection construction production will be set after the completion of the handshake SYN flag is set to 0.

    Termination FIN: for releasing a connection. FIN = 1 represents: a data sender of this segment has been transmitted, and for the release of the transport connection

    PS: ACK, SYN and FIN flags of these capitalized words represent bits whose value is either 1 or a 0; ack, seq lowercase number word representation.

    Field Meaning
    URG urgent pointer is valid. 1, showing a bit to be prioritized
    ACK acknowledgment number is valid, is generally set to 1.
    PSH prompts the receiver application immediately go read data from the TCP buffer.
    RST other requirements to re-establish a connection reset.
    SYN requests to establish a connection, and the initial value setting its sequence number in a sequence number field. Establish a connection, is set to 1
    FIN want to disconnect.
Three-way handshake understand


 

The first handshake: connection is established, the client sends syn packets (syn = x) to the server, and enters the SYN_SENT state, waiting for the server to confirm; the SYN: synchronization sequence number (Synchronize Sequence Numbers).

Second handshake: server receives syn packets, must confirm the customer SYN (ack = x + 1), while themselves sends a SYN packet (syn = y), i.e., SYN + ACK packet, then the server enters a state SYN_RECV;

Third handshake: the client receives the SYN + ACK packet to the server, the server sends an acknowledgment packet ACK (ack = y + 1), this packet is sent, the client and server into the ESTABLISHED (TCP connection succeeds) state, complete the three handshake.

Four waving process understanding 


1) The client process issues a connection release message and stops sending data. Releasing the data packet header, FIN = 1, the sequence number seq = u (equal to the sequence number of the last byte previously transmitted from the data plus 1) In this case, the client enters the FIN-WAIT-1 (termination waiting 1) state. TCP provisions, FIN segment, if not carrying data, but also consume a serial number.
2) the server receives a connection release message, a confirmation message, ACK = 1, ack = u + 1, and bring its own sequence number seq = v, case, the server proceeds to the CLOSE-WAIT (Close Wait )status. TCP server to inform high-level application process, the client to the server on the release direction, this time in a semi-closed state, that is, the client has no data to send, but if the server sending data, the client still has to be accepted. This state will continue for some time, that is, the entire CLOSE-WAIT state duration.
3) Client receives acknowledgment requests the server side, in which case, the client enters the FIN-WAIT-2 (2 termination waiting) state, waiting for the last packet the server transmits a connection release (also need to agree before the server sends the data).
4) the server sends the last data is completed, the client sends a connection release message, FIN = 1, ack = u + 1, since the half-closed state, and the server is likely to transmit some data, it is assumed in this case serial No. seq = w, at this time, the server into the lAST-ACK (acknowledgment last) state, waiting for an acknowledgment of the client.
5) After the client receives a connection release message server must send acknowledgment, ACK = 1, ack = w + 1, and their serial number seq = u + 1, In this case, the client enters the TIME- wAIT (wait) state. Note that the TCP connection has not been released at this time, the must be 2 ** MSL (maximum segment lifetime) time, when the client undo the TCB corresponding, before entering the CLOSED state.
6) As long as the server has received confirmation sent by the client, immediately enter CLOSED state. Similarly, after the revocation of TCB, it is over the TCP connection. You can see, the end of time than client server TCP connection ends earlier.

 常见面试题
【问题1】为什么连接的时候是三次握手,关闭的时候却是四次握手?

答:因为当Server端收到Client端的SYN连接请求报文后,可以直接发送SYN+ACK报文。其中ACK报文是用来应答的,SYN报文是用来同步的。但是关闭连接时,当Server端收到FIN报文时,很可能并不会立即关闭SOCKET,所以只能先回复一个ACK报文,告诉Client端,"你发的FIN报文我收到了"。只有等到我Server端所有的报文都发送完了,我才能发送FIN报文,因此不能一起发送。故需要四步握手。

【问题2】为什么TIME_WAIT状态需要经过2MSL(最大报文段生存时间)才能返回到CLOSE状态?

答:虽然按道理,四个报文都发送完毕,我们可以直接进入CLOSE状态了,但是我们必须假象网络是不可靠的,有可以最后一个ACK丢失。所以TIME_WAIT状态就是用来重发可能丢失的ACK报文。在Client发送出最后的ACK回复,但该ACK可能丢失。Server如果没有收到ACK,将不断重复发送FIN片段。所以Client不能立即关闭,它必须确认Server接收到了该ACK。Client会在发送出ACK之后进入到TIME_WAIT状态。Client会设置一个计时器,等待2MSL的时间。如果在该时间内再次收到FIN,那么Client会重发ACK并再次等待2MSL。所谓的2MSL是两倍的MSL(Maximum Segment Lifetime)。MSL指一个片段在网络中最大的存活时间,2MSL就是一个发送和一个回复所需的最大时间。如果直到2MSL,Client都没有再次收到FIN,那么Client推断ACK已经被成功接收,则结束TCP连接。

【问题3】为什么不能用两次握手进行连接?

答:3次握手完成两个重要的功能,既要双方做好发送数据的准备工作(双方都知道彼此已准备好),也要允许双方就初始序列号进行协商,这个序列号在握手过程中被发送和确认。

       现在把三次握手改成仅需要两次握手,死锁是可能发生的。作为例子,考虑计算机S和C之间的通信,假定C给S发送一个连接请求分组,S收到了这个分组,并发 送了确认应答分组。按照两次握手的协定,S认为连接已经成功地建立了,可以开始发送数据分组。可是,C在S的应答分组在传输中被丢失的情况下,将不知道S 是否已准备好,不知道S建立什么样的序列号,C甚至怀疑S是否收到自己的连接请求分组。在这种情况下,C认为连接还未建立成功,将忽略S发来的任何数据分 组,只等待连接确认应答分组。而S在发出的分组超时后,重复发送同样的分组。这样就形成了死锁。

【问题4】如果已经建立了连接,但是客户端突然出现故障了怎么办?

TCP还设有一个保活计时器,显然,客户端如果出现故障,服务器不能一直等下去,白白浪费资源。服务器每收到一次客户端的请求后都会重新复位这个计时器,时间通常是设置为2小时,若两小时还没有收到客户端的任何数据,服务器就会发送一个探测报文段,以后每隔75秒钟发送一次。若一连发送10个探测报文仍然没反应,服务器就认为客户端出了故障,接着就关闭连接。

Guess you like

Origin www.cnblogs.com/wjqhuaxia/p/11789442.html