TCP/IP

First of all, what is TCP/IP

TCP/IP transport protocol, namely transmission control/network protocol, also known as network communication protocol. It is the most basic communication protocol in the use of network.Copy the code

So if you’re communicating you’re going to have to connect so how does TCP/IP connect?

This leads to another concept :(TCP/IP) packets

A packet is divided into two parts: the header and the data. A packet is called a packet only when the two parts are connected

Aside from ports and data, the most important thing is reserved, and reserved for defining purposes, flags: 6-bit flag fields. It can be emergency flag, meaningful reply flag, push flag, reset connection flag, synchronization serial number flag, and completion of sending data flag. The sequence is URG, ACK, PSH, RST, SYN, and FIN.

In plain English:

SYN(synchronous), establishing connection.

ACK (acknowledgement), confirmed.

PSH (push), and transmission.

FIN (finish), over.

RST (reset), reset.

URG (urgent), emergency.

(Note this later)

Getting back to the subject, let’s talk about three handshakes and four waves:

TCP three-way handshake

Three-way Handshake Establishes a TCP connection. The client and server send Three packets to confirm the establishment of a TCP connection. The whole process is shown in the figure below:

(1) The first handshake:

The Client sets the SYN flag bit to 1, randomly generates a value seq=J, and sends the packet to the Server. The Client enters the SYN_SENT state and waits for the Server to confirm the packet.

(2) Second handshake:

When the Server receives the packet, the flag bit SYN=1 knows that the Client requests 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 packet to the Client to confirm the connection request. The Server enters the SYN_RCVD state.

(3) The third handshake:

After receiving the confirmation, the Client checks whether the ACK is J+1 and ack is 1. If yes, the Client sets the flag ACK bit to 1, ack=K+1, and sends the packet to the Server. The Server checks whether the ACK is K+1 and ACK is 1. The Client and Server enter the ESTABLISHED state and complete the three-way handshake. Data can be transmitted between the Client and Server.

Three handshakes to establish a connection

First handshake: The client communicates with the server by sending a SYN=1 request, “Server, I want to talk to you. Can I connect now?”

Second handshake: When the server receives a connection request from the client, it sends an acknowledgement message to the client: “I know (ACK), I am ready, can you connect now (SYN)”.

Third handshake: When the client receives the connection confirmation from the server, it politely informs the server, “OK, let’s start connecting (ACK)”.

By this time, the whole process of establishing the connection is over, and the next is the process of communication and even simultaneous transmission of information.

TCP waved four times

The so-called four-way Wavehand refers to the termination of a TCP connection. When a TCP connection is disconnected, the client and server need to send a total of 4 packets to confirm the disconnection. The whole process is shown in the figure below:

The TCP connection is in full duplex, so each direction must be closed separately. This principle is that when a party finishes sending data, it sends a FIN to terminate the connection in this direction. Receiving a FIN only means that no data flows in this direction, that is, no more data is received. However, data can still be sent on this TCP connection until a FIN is also sent in that direction. The party that closes first performs an active shutdown, while the other party performs a passive shutdown, as depicted in the figure above.Copy the code

(1) First wave:

The Client sends a FIN to stop data transmission from the Client to the Server, and the Client enters the FIN_WAIT_1 state. (Status bit FIN=1, send SEq =J)

(2) Second wave:

After receiving a FIN, the Server sends an ACK to the Client, confirming that the sequence number is +1 (the same as that for SYN, one FIN occupies the same sequence number). The Server enters CLOSE_WAIT state. (Status bit ACK=1, send ACK= J+1)

(3) The third wave:

The Server sends a FIN to disable data transfer from the Server to the Client, and the Server enters the LAST_ACK state. (Status bit FIN=1, send SEq =K)

(4) The fourth wave:

After receiving the FIN, the Client enters the TIME_WAIT state and sends an ACK to the Server to confirm that the FIN number is +1. The Server enters the CLOSED state and waves four times. (Status bit ACK=1, ACK= K+1)

Four waves of disconnection

First wave: The communication between the two sides is almost complete. At this point, the client has ended the communication connection, so it tells the server “I’m finished (FIN)”, and then it forms the state of waiting to end the connection.

Second wave: the server knows the client has no words, the server at this time there are two words to the client to say, “I know you finished (ACK), I will give you two words, &*…… % RMB “.

Third wave: at this point, the client continues to wait for the end of the state, the server is finished, it is waiting to close the connection, and tells the client, “I’m done, let’s break (FIN)”.

Fourth wave: Client know the service side also had said, also want to tell the server (ACK), because both the connect and disconnect to press close to disconnect, client and define a timer for yourself at the same time, because they don’t know what I said just now that sentence can accurately reach the server (network instability or other factors caused by the network), The default time is the sum of the maximum time for two communications. If the time exceeds this time, the server has received its own confirmation message by default, and the client closes its own connection. Once the server receives the confirmation notification from the client, the server immediately closes the connection.

This is the end of the whole communication process between the two parties. Here to declare: disconnection is not necessarily the client, who can initiate the disconnection instruction first, and there is no fixed standard between the client and the server, who initiated the request first is the client.Copy the code