TCP Three-time handshake, four wave and diagram with HTTP connection

Source: Internet
Author: User

Reference blog:

http://blog.csdn.net/whuslei/article/details/6667471

Http://www.2cto.com/net/201310/251896.html

(i) TCP establishment process (three handshake)

The so-called three-time handshake (three-way handshake) establishes a TCP connection, which means that when a TCP connection is established, a total of 3 packets are sent by the client and the server to confirm the establishment of the connection.

(1) First handshake: The client will set the flag bit SYN to 1, randomly generate a value seq=j, and send the data packets to server,client into the syn_sent state, waiting for the server to confirm.
(2) Second handshake: After the server receives the packet by the flag bit syn=1 knows the client request to establish a connection, the server sets the flag bit SYN and ACK to 1,ack=j+1, randomly generates a value seq=k, and sends the data packets to the client to confirm the connection request , the server enters the SYN_RCVD state.
(3) Third handshake: After the client receives the acknowledgment, checks whether the ACK is j+1,ack 1, and if correct, resets the flag bit ACK to 1,ack=k+1 and sends the data packets to Server,server to check if the ACK is K+1,ack 1, If the connection is successful, the client and server enter the established state, complete three handshake, and then the client and server can start transmitting data.

(ii) TCP disconnect process (four waves)

"Note" The interrupt connection can be either client side or server side.

Suppose the client side initiates an interrupt connection request, which is to send a fin message. After the server receives the fin message, it means "My client has no data to send to you", but if you have data that is not sent, you do not need to close the socket, you can continue to send data. So you first send an ACK, "tell the client that your request I received, but I am not ready, please continue to wait for my message." At this point the client enters the fin_wait state and continues to wait for Fin messages on the server side. When the server side determines that the data has been sent, the fin message is sent to the client side, "Tell the client side, OK, my side of the data is finished, ready to close the connection." Client side received fin message, "I know can shut down the connection, but he still do not believe the network, afraid the server side do not know to shut down, so send an ACK into the time_wait state, if the server does not receive an ACK can be re-transmitted. "When the server side receives an ACK," You know you can disconnect. " Client side waiting for 2MSL still not received a reply, the server side has been properly shut down, well, I can also close the client terminal connection. The OK,TCP connection is closed like this!

Because the TCP connection is full-duplex, each direction must be closed separately, the principle is that when a party completes the data sending task, send a fin to terminate the connection in this direction, the receipt of a fin just means that there is no data flow in this direction, no longer receive data, However, the data can still be sent on this TCP connection until fin is sent in this direction. The first party to close will perform the active shutdown, while the other side performs a passive shutdown, as described.
(1) First wave: The client sends a fin to turn off the client to server data transfer, the client enters the fin_wait_1 state.
(2) Second wave: After receiving fin, the server sends an ACK to the client, confirming that the sequence number is received sequence number +1 (same as SYN, one fin occupies a serial number), and the server enters the close_wait state.
(3) Third wave: The server sends a fin to shut down the server-to-client data transfer, and the server enters the Last_ack state.
(4) The fourth wave: After the client receives fin, the client enters the TIME_WAIT state, and then sends an ACK to the server, confirming that the serial number is received +1,server enter the closed state, complete four waves.

(iii) HTTP and TCP relationships

Why is HTTP a stateless short connection, and TCP is a stateful long connection? HTTP is not built on the basis of TCP, why can it be a short connection? Now it is clear that HTTP is shutting down the TCP connection after each request is completed, so it is a short connection. When we use the TCP protocol directly through the socket programming, we can control when the connection is closed by the code area, as long as we do not close the connection through the code, the connection will persist in the process of the client and server, and the related state data will be kept.

TCP Three-time handshake, four wave and diagram with HTTP connection

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.