The connection process of the TCP protocol (data transport layer) [three-way handshake and four farewells]

Some time ago, I learned the OSI network model and felt that I understood it. It is still very simple. If you want to study these two in depth, I will write my heart here for your reference and correction.
Introduction
TCP is a connection-oriented protocol. Before either party can send data to the other, a connection must be established between the two parties. In this respect, unlike connectionless protocols such as UDP, UDP sends datagrams to one end without any handshake. 1. Content of TCP
segment

The connection process of the TCP protocol (data transport layer) [three-way handshake and four farewells]

The IP header is the IP address of the destination and source. The focus here is that the TCP header data format is as follows. The optional field is usually 20 bytes if not counted.
The connection process of the TCP protocol (data transport layer) [three-way handshake and four farewells]

Sequence number: Seq sequence number, occupying 32 bits, is used to identify the byte stream sent by the TCP source to the destination, and the initiator sends data to mark this.
Acknowledgement sequence number: ack sequence number. The acknowledgement sequence number segment is valid only when the ACK flag is 1.
There are 6 ack=Seq+1 flag bits:
URG: The emergency pointer is valid
ACK: The acknowledgment sequence number is valid
PSH: The receiver should quickly Give this message to the application layer for processing
RST: reset the link
SYN: initiate a new connection
FIN: release a connection
Notes:
1) Do not confuse the confirmation sequence number ack (lowercase) with the confirmation flag bit ACK
2) Confirmer ack=Initiator Seq+1 The two ends are paired with
two or three handshakes. The
so-called Three-way Handshke is to establish a TCP connection, which means that three packets are sent to each other to determine the establishment of the connection. (In the socket transformation, this process is triggered by the client executing connetc, which is used in C language too bind)
The connection process of the TCP protocol (data transport layer) [three-way handshake and four farewells]

Three-way handshake:
1) The client sets the flag SYN to 1, and randomly generates a value Seq=x
2) The server receives the data packet and knows that the client requests to establish a connection from the flag bit SYN=1. The server sets the flag bits SYN and ACK to 1 After ack=x+1, a seq=y is randomly generated. And send the data to the client to confirm the request
3) After the client receives the confirmation, check whether the ack is x+1. Whether ACK is 1. If the correct side sets ACK to 1 then ack=y+1. Send data to server. Server checks if ack is y+1. Whether the ACK is 1 and if it is correct, the connection is established successfully.
Three or four waves
The connection process of the TCP protocol (data transport layer) [three-way handshake and four farewells]

Since the TCP connection is full-duplex, each direction must be closed separately. This principle is that when one party completes the data transmission task, it sends a FIN to terminate the connection of this party. Receiving FIN only means that this party There is no data flow up, that is, no data is received but data can still be sent on the TCP connection until FIN is also sent in this direction. The side that shuts down first will perform an active shutdown and the other side will perform a passive shutdown.
1) The client sends a FIN to close the data transfer from the client to the server.
2) After the server receives a FIN, it sends an ACK to the client, confirming that the serial number is the received serial number + 1 (same as SYN, one FIN occupies one serial number ),
3) The server sends a FIN to close the data transmission from the server to the client
4) After the client receives the FIN, the client sends an ACK to the server to confirm that the serial number is the received serial number + 1
, and waved goodbye four times.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343883&siteId=291194637