You have to know the three-way handshake with the four waving! ! !

I. Description

Here Insert Picture Description

  • 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 will only be set when the TCP connection completion, 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.
    Here Insert Picture Description

II. Three-way handshake understand

Here Insert Picture Description

  • 小明:我爱你

  • 小美: 其实......我也爱你

  • 小明:那我们在一起吧!

  • 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.

IV. Four waving process understanding

Here Insert Picture Description

  • 小明:我不爱你了
  • 小美:渣男,我早就知道了,呜呜呜 ~~~
  • 小美:那我们分手吧!
  • 小明:好聚好散
  • The client process makes 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.
  • 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 (closed waiting) . 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.
  • When the client receives the server request confirmation, in which case, the client enters the FIN-WAIT-2 (2 termination waiting) state, waiting for the server to send a connection release message (this also need to agree before the last data sent by the server) .
  • After the server transmits the final 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, the sequence number is assumed at this time to seq = w, at this time, the server into the lAST-ACK (acknowledgment last) state, waiting for an acknowledgment of the client.
  • 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 ( time waiting) 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.
  • As long as the server 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.

IV. Common interview questions

  • Why when the connection is three-way handshake, when it is closed handshake?
    A: Because when the terminal receives SYN Server Client terminal connection request message may be sent directly SYN + ACK packet. Wherein the ACK message is used for response, SYN etc is used to synchronize the packet. But the connection is closed, when the Server side FIN packet is received, probably does not close immediately SOCKET, it can only respond to a first ACK packet, told Client-side, "you send FIN messages I have received." Only until the end of all my Server messages are sent over, I can send FIN packets, and therefore can not be sent together. It requires four-way handshake.

  • Why go through the TIME_WAIT state 2MSL (maximum segment lifetime) CLOSE to return to the state?
    A: Although Logically, the four packets have been sent, we can go directly to the CLOSE state, but we have the illusion of a network is unreliable, it is possible to lose the last ACK. So TIME_WAIT state is used to retransmit ACK packets may be lost. Send out the final ACK reply Client, but the ACK may be lost. Server If no ACK, FIN segment sent will repeat. Client can not close it immediately, it must be confirmed Server receives the ACK. Client enters the TIME_WAIT state after sending the ACK. Client will set a timer, waiting 2MSL time. If you receive FIN again within that time, then the Client will resend ACK 2MSL and wait again. The so-called 2MSL is twice the MSL (Maximum Segment Lifetime). MSL refers to a segment of the network the maximum survival time, 2MSL is transmitted and a maximum time required for a response. If until 2MSL, Client have not received FIN again, then the Client concluded that ACK has been successfully received, the TCP connection.

  • Why not connect with two-way handshake?
    A: 3-way handshake to complete two important functions, both sides should make preparations to send data (both sides know each other ready), but also to allow the parties to negotiate on the initial sequence number, serial number during the handshake It is sent and acknowledged.
    Now put into a three-way handshake only requires two hands, a deadlock is likely to occur. As an example, consider the communication between the computer C and S, S is assumed to C transmits a connection request packet, the received packet is S, and send the acknowledgment packet. Under the agreement the two shook hands, S considers the connection has been successfully established, you can start sending data packets. However, C in the case of S reply packet is lost in transmission, and will not know if S is ready, I do not know what kind of establishment S serial number, C S even doubt whether it has received its own connection request packet. In this case, C considers the connection has not been established successfully, will ignore any data packet sent by S, just waiting for the connection confirmation response packet. After the S timeout packet sent repeatedly transmits the same packet. This creates a deadlock.

  • How to do if you have established a connection, but the client suddenly broke down?
    A: TCP also has a keep-alive timer, apparently, a client if a failure occurs, the server can not keep on waiting, wasted resources. Each time the server receives the client's request will reset the timer set time is usually 2 hours, if two hours has not received any data from the client, the server sends a detection message segment, then every 75 seconds to send one. If sending 10 consecutive probe packets still did not respond, the server brought out customers considered a failure, and then they close the connection.

Published 70 original articles · won praise 4 · Views 6346

Guess you like

Origin blog.csdn.net/qq_44837912/article/details/104727087