Why is TCP doing three handshakes and four waves?

Source: Internet
Author: User
Tags ack
TCP is the host to the host Layer Transmission Control Protocol, providing a reliable connection service, using three times handshake confirmation to establish a connection. (1) Bit code is the TCP flag bit:There are 6 kinds of signs: SYN (synchronous online), ACK (Acknowledgement confirmation), PSH (push transfer, ST (reset), URG (Urgent emergency), Sequence number (sequential numbers) Acknowledge number (confirmation). (2) three times handshake for the first time: Host a send bit code for syn=1, randomly generated SEQ number=1234567 packets to the server, Host B by the syn= host; second handshake: Host B receives the request to confirm the online information, sends an ACK to a number= (host A's seq+1), syn=1,ack=1, with 1 of the package third handshake: Host a received after checking ACK number is correct, that is, the first sent Seq Number+1, as well as the bit-code ACK for Resend ACK number= (Host B seq+1), ack=1 , Host B confirms that the SEQ value and Ack=1 are successful when received.    Completes three handshake, host A and Host B begin to transmit data.   In the TCP/IP protocol, the TCP protocol provides a reliable connection service, and a three-time handshake is used to establish a connection. (1) The first handshake: when the connection is established, the client sends the SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm; (2) The second handshake: the server receives the SYN packet, must confirm the customer's SYN (ACK=J+1), At the same time send a SYN packet (syn=k), when the server into the Syn_recv state; (3) The third handshake: the client receives the server Syn+ack packet, sends the confirmation packet ack to the server (finished, the client and the server enters the established state, completes three times handshake. Completes three handshake, the client and the server begin to transmit the data. (3) four times waveBecause the TCP connection is Full-duplex, each direction must be closed separately. Receiving a fin only means that there is no data flow in this direction, and a TCP connection can still send data after receiving a fin.  The side that first closes will perform a passive shutdown.  (1) Client A sends a fin to turn off client A to Server B for data transfer (message segment 4). (2) Server B receives this fin, it sends back an ACK, confirming that the serial number is the received number plus 1 (message Segment 5). Like Syn, a fin will occupy (3) Server B closes the connection to client A and sends a fin to client a (message segment 6). (4) Client A sends back ACK message confirmation and sets the confirmation number to receive the serial number plus 1 (message segment 7).
State Detailed: CLOSED:There's nothing to say about this, the initial state. LISTEN:This is also very easy to understand a state, indicating that a server side of the socket is in the listening state, you can accept the connection. SYN_RCVD:This state is accepted to the SYN message, under normal circumstances, this state is a server-side socket in the process of establishing a T session in a middle state, very short, basically with netstat you are very difficult to see this state, Unless you specifically wrote a client test the last ACK message in the CP handshake is not sent. Therefore, when the client's ACK message is received, it will enter the Estab. syn_sent:This state echoes with SYN_RCVD, when the client socket executes the connect connection, it first sends it into the syn_sent state and waits for the 2nd message in the service side to send a three handshake. fin_wait_1:The difference between these two states is that the fin_wait_1 state is actually when the socket is in the established state, it wants to turn off the connection actively, at which point the socket enters the fin_wait_1 state. And when the other side response ACK message, then into the fin_wait_2 state, now, no matter what the other circumstances, should immediately respond to ACK message, so fin_wait_1 state is generally more difficult to see, and Fin_wa can be seen with netstat. fin_wait_2:This state has been explained in detail above, in fact, the fin_wait_2 state of the socket, which means a half connection, but also to tell the other side, I have some data to be sent to you, and then close the connection. time_wait:Indicates that the other side of the fin message, and sent an ACK message, and so on 2MSL can return to closed available wait_1 state, received the other side with the Fin logo and ACK sign message, you can directly into the TIME_WAIT state, without state. CLOSING:This kind of state is very special, the actual situation should be very rare, belong to a relatively rare exception state. Under normal circumstances, when it is supposed to be received (or at the same time) the other side of the ACK message, and then receive each other's fin message. But the closing state indicates that you have sent an ACK message to the other side, but you have also received a fin message from the other side. Under what circumstances would such a situation arise? Actually think about it, also not rare both sides almost at the same time close a socket, then there are both sides send fin message, also will appear closing closed socket connection. close_wait:The meaning of this state is actually that it is waiting to be closed. How to understand it. When the other side close a socket after the sending system will undoubtedly respond to an ACK message to the other side, then into the close_wait state.
Summary: 1.     Why the Connection agreement is three times handshake, and closing the connection is four times waving. This is because the socket in the listen state of the service port when the SYN message is received, it can send ack and SYN (ACK up response, and SYN to sync) in a message. But when you close the connection, when you receive the other's fin message notification, it simply means that the other party has no data to send to you, but not all of your data are sent to each other, so you may not immediately close the socket, that is, you may also need to send some data to each other after the  Send a FIN message to the other person to show that you agree now but here the ACK message and fin message are sent in most cases separately. 2.     Why the TIME_WAIT state also needs to wait 2MSL before returning to the closed state. This is because although both parties agree to close the connection, and the 4 packets of the handshake are coordinated and sent over, it is logically possible to return directly to the closed state (as in the Stablish state), but because we have to pretend that the network is unreliable, You can not guarantee that your final ACK message will be received by the other side, because the state of the socket may be due to timeout did not receive an ACK message, and resend fin message, so the role of this time_wait state is used to resend the possibility of missing ACK messages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.