TCP provides reliable transmission by using three-time handshakes to establish a reliable connection when establishing a connection.
In socket programming, the client executes connect (). Will trigger a three-time handshake.
Example: Host a runs the client, b runs the server program, the TCP processes at both ends belong to the closed (off) state, a actively opens the connection, B passively opens the connection
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/35/wKiom1c6_5HQo-8hAABRT-KHACw964.png "title=" 07.png "alt=" Wkiom1c6_5hqo-8haabrt-khacw964.png "/>
First connection: Client A sends a connection request segment to Server B, at which time the sync bit syn is set to 1 ( no data, only one serial number ), and an initial serial number is stored in the Header Serial Number field.
Second connection: After the server has received the connection request message segment, if the connection is accepted, the client is sent a confirmation, the SYN and ACK bits are set to 1 in the acknowledgment message segment, the confirmation number is x+1, indicating that all the data was received before it, and the initial serial number of its own
Third connection: The client receives the confirmation from the server side, confirms to the server side again, confirms that the message segment's ACK is 1,a enters the connected state, the server receives the confirmation from the client, also enters the established connection state
Why connect three times: Prevent the failed connection request message segment from being suddenly uploaded to the server side
Example: If a send the first connection request message segment in some network nodes to stay longer, resulting in a time after the connection is released to the server side, at this time the message segment has been invalidated, but the server side received the failed connection request message segment, mistakenly think that the client has issued a new connection request. The client is then sent a confirmation message segment, agreeing to establish a connection. However, because the client does not make a connection request, but the server side thinks that the new connection has been established and waits for the client to send the data, the server-side resources are wasted
Connection release for TCP: four-time wave mechanism
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/35/wKiom1c7B8zikkvFAABPOxFwE9w460.png "title=" 7200022.png "alt=" Wkiom1c7b8zikkvfaabpoxfwe9w460.png "/> For the first time: The client sends a connection release segment to its TCP, stops sending data, and actively closes the link.client will connect to release packet header end control bit fin Set 1
Second: The server received this fin, it sent back an ACK acknowledgement, confirm the sequence number for the received sequence number plus 1, a fin occupies a sequence number, the server side into the shutdown wait state, the TCP server notifies the application process, at this time from the customer to the server in this direction of the connection is released, At this point, TCP is in a semi-shutdown state (the client is not sending data at this time, if the server is sending data, the customer still receives)
third time: The server has no data sent to release the connection to the client, sending a fin to the client.
fourth time: The client sends ACK message acknowledgement and sets the confirmation sequence number to receive the serial number plus 1. The TCP connection is not released at this time, and the client will not be in the shutdown state until after the timer has been set .
Connection establishment and connection release for TCP