TCP (Transmission Control Protocol) Transmission Control Protocol
TCP is the transmission control protocol used by the host to control the host layer. It provides reliable connection services and uses three handshakes to establish a connection:
The location code is the TCP flag, which has six types of labels:
Syn (synchronous online)
Ack (acknowledgement confirmation)
Psh (push transfer)
FIN (finish)
RST (reset)
URG (Urgent urgent)
Sequence Number)
Acknowledge number (confirmation number)
TCP packet header
Establish a connection over TCPShows the TCP connection establishment process. A actively opens the service, and B passively opens the service. the TCP server process of B first creates the transmission control block TCB (Transmission Control Block stores some important information in each connection, such as: TCP connection table, pointer to the sending and receiving cache, pointer to the retransmission queue, the current sending and receiving serial number, etc.). Prepare to receive the connection request from the client process, and then the server process is in the listen status, waiting for the customer's connection request. if yes, make a response. a's tcp client process also first creates a transmission control block and then sends a connection request packet segment to B. At this time, the synchronization bit SYN in the header is 1 and an initial sequence number seq = x. According to TCP rules, the SYN packet segment (SYN = 1) cannot carry data, but consumes a serial number. at this time, the tcp client process enters the SYN-SENT state. 2. if B agrees to establish a connection after receiving the connection request message segment, it will send confirmation to. in the validation packet segment, both SYN and ACK bits should be set to 1, the confirmation number is ACK = x + 1, and an initial sequence number seq = y should also be selected for you. This packet segment cannot carry data and consumes a serial number. the TCP server process enters the SYN-RCVD (synchronously received) state. 3. after receiving confirmation from B, the tcp client process should also confirm with B. Set ack to 1, ack to Y + 1, and SEQ to x + 1. According to TCP standards, ACK packets can carry data. However, if no data is carried, no sequence number is consumed. In this case, the sequence number of the next data packet segment is still seq = x + 1. At this time, the TCP connection has been established, and a enters the established (established connection) status.
When B receives a's confirmation, it also enters the established status.
Why is there a third confirmation?This is mainly to prevent the invalid connection request packet segment from being suddenly transmitted to B, resulting in an error. Assume that there is no client for the third confirmation. A sends a connection request, but the connection request is lost without receiving B's confirmation. So a re-sends a request for connection. Then B receives the request and sends a confirmation to establish a connection, after the communication is completed, the connection is released and a communication is successfully completed. Although a has sent two connection requests, it is lost for the first time and succeeded for the second time. Therefore, no invalid connection request message exists. Assume that the connection request message sent by a for the first time is stuck due to a network problem for a long time and reaches B only after the connection is released for the second communication. This is an invalid message, but at this time, B thought it was another connection request from a, so a confirmation was sent. If there was no third confirmation, the connection was successfully established. Because a has not issued a establishment request, B's reply confirmation A ignores it, so it does not communicate with B. However, B has been waiting for a to send data to him, B waited for a long time and wasted his youth (B's resources ).
TCP release connection1. After the data transmission is complete, both parties can release the connection. Currently, both A and B are in the established state. A sends a connection to release the packet segment, stops sending data, and closes the TCP connection. A sets the first part of the connection release packet segment to fin 1. Its sequence number is seq = U, and U is equal to the sequence number of the last byte of the previously sent data plus 1. this is the status of a entering fin-wait-1 (Termination wait 1), waiting for B's confirmation. TCP stipulates that even if the FIN packet segment does not carry data, it consumes a sequence number. 2. B sends a confirmation after receiving the connection release packet segment. The confirmation number is ACK = u + 1, and the serial number of this packet segment is v, equal to the number of the last byte of the data sent before B plus 1. then B enters the close-Wait (close Wait) status. So far, the-> B connection is released, and the TCP connection is in the half-close state, that is, a has no data to send, but if B sends data, A still receives the message. 3. After receiving confirmation from B, A enters the fin-wait-2 (Stop wait 2) Status and waits for B to release the packet segment from the connection. If B has no data to send to a, its application process notifies TCP to release the connection. At this time, the connection sent by B must make fin = 1. Assume that the serial number of B is W (B may have sent some data in the semi-closed status ). B must also confirm the number ACK = u + 1 that has been sent from the husband. At this time B enters the LAST-ACK status, waiting for a to confirm. 4. A must confirm after receiving B's connection release packet segment. In the validation packet segment, set ack to 1, ack to W + 1, and SEQ to U + 1 (the FIN packet segment sent earlier consumes one serial number ). Then enter the time-Wait Status. The TCP connection has not been released yet. A enters the closed State only after 2msl is set by the time-Wait timer (time-Wait timer. (MSL: maximum segment lifetime maximum message segment life) Why does it have to wait in time-Wait? 1. To ensure that the last ACK packet segment sent by a can reach B. This ACK packet segment may be lost, so that B in the LAST-ACK state will not receive confirmation. B will retransmit the FIN + ACK packet segment upon timeout, and a will be able to receive the FIN + ACK packet segment of the retransmission within 2msl, and then a will re-Send the packet to confirm and restart the timer. Preferably, AB enters the closed State normally. If a does not wait for a while in the Time-Wait Status, but immediately releases the connection after the ACK packet is sent, it cannot receive the FIN + ACK packet segment sent by B, therefore, no confirmation message is sent. In this way, B cannot enter the closed state according to the normal steps. 2. Prevent invalid connection request packets from appearing in this connection.
Http://blog.chinaunix.net/uid-26413668-id-3376762.html
TCP three-way handshake, four breaks up