The
tcp three-time handshake tcp connection is initialized by a three handshake. The purpose of the three handshake is to synchronize the serial number and confirmation number of both sides of the connection and to Exchange TCP window size information. The following steps outline the process by which a client computer typically contacts a server computer: The 1. client sends a SYN-bit TCP message to the server, It contains the initial serial number x and a window size for the connection (representing the size of the buffer on the client to store incoming segments sent from the server). After receiving the SYN message sent by the client, the 2. server sends a TCP message with both the SYN and ACK bits set to the client, which contains the initial serial number y of its choice and the confirmation of the serial number of the client x+ 1 and a window size (representing the size of the buffer on the server that is used to store incoming segments sent from the client). 3. When the client receives the Syn+ack message returned by the server, it returns a confirmation number y+1 and a serial x+1 ACK message to the server side, and a standard TCP connection completes. tcp uses a similar handshake process to end the connection. This ensures that all two hosts can complete the transfer and that all data is received
TCP Client |
Flags |
TCP Server |
1 Send SYN (seq=x) |
----SYN---> |
SYN Received |
2 Syn/ack Received |
<--syn/ack-- |
Send SYN (seq=y), ACK (x+1) |
3 Send ACK (y+1) |
----ACK---> |
ACK Received, Connection established |
W:ISN (Initial Sequence number) of the Client |
X:isn of the Server |
A detailed explanation of TCP/IP Protocol connection establishment and release
In the TCP/IP protocol, the TCP protocol provides a reliable connection service, using a three handshake to establish a connection, as shown in Figure 1.
(1) First handshake: When establishing a connection, client A sends a SYN packet (SYN=J) to Server B and enters the syn_send state, waiting for Server B to confirm.
(2) The second handshake: Server B received the SYN packet, must confirm customer a SYN (ACK=J+1), and also send a SYN packet (syn=k), that is Syn+ack packet, when server B into the SYN_RECV state.
(3) Third handshake: Client A receives Server B's Syn+ack packet, sends the confirmation packet ACK (ack=k+1) to Server B, this packet sends over, the client A and Server B enter established state, completes three times handshake.
Completes three handshake, the client and the server begin to transmit the data.
Figure 1 TCP Three-time handshake Establish connection
Because the TCP connection is Full-duplex, each direction must be closed separately. The principle is that when one party completes its data sending task, it sends a fin to terminate the connection in this direction. 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 the active shutdown while the other side performs the 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 an ordinal number.
(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).
TCP uses four waves to close the connection as shown in Figure 2.
Figure 2 TCP Four-time wave close connection
1. Why the Connection agreement is three times handshake, and closing the connection is four times handshake.
This is because the socket in the listen state of the server when the SYN message is received, it can send ack and SYN (ACK to respond, and SYN to sync) in a message. But when you close the connection, when receiving a fin message from each other, it simply means that the other party has no data to send to you, but not all of your data is sent to each other, so you may not be able to immediately close the socket, that is, you may also need to send some data to each other after the Send fin message to each other to indicate that you agree that the connection can now be closed, so it's ACK and fin messages 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, it is logically possible to return directly to the closed state (as it is from the Syn_send state to the establish 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, so the other side in the Last_ack state of the socket may be due to timeout did not receive an ACK message, and resend the fin message, so the role of this time_wait state is to resend the likely loss of the ACK message.
This digest is from:
The analysis of the state transition diagram of TCP connection
Http://www.diybl.com/course/6_system/linux/Linuxjs/2007104/76065.html
Reference:
"TCP/IP detailed-Volume 1" 18th chapter "TCP Connection establishment and termination"
Http://course.ccniit.com/CSTD/Linux/reference/files/018.PDF