Transport layer protocol: TCP
1. TCP protocol
1. TCP protocol
TCP protocol is a connection-oriented protocol that provides reliable transmission services
2. TCP port number
Function: Used to distinguish different network services
Classification of ports: well-known ports and dynamic ports
Well-known ports: 0-1023, ports used by common applications
Dynamic port: 1024-65535, generally used as the source port
3. The protocol corresponding to the port number
1. TCP header
The entire TCP header is 20 bytes in total
Source Port: source end
Destination Port: destination port
Sequence Number: sequence number
Acknowledge Number: Confirmation number
Header Length; header length
Resv: reserved field
SYN: Indicates establishing a connection
ACK: indicates response
Window: window, used for flow control
Checksum: Checksum field bit code is the tcp flag bit. There are 6 types of marks: SYN (synchronous establishment of connection) ACK (acknowledgement confirmation) PSH (push transmission) FIN (finish end) RST (reset reset) URG (urgent emergency)
4. TCP three-way handshake
The first handshake: Host A sends the bit code syn=1 (indicating a request to establish a connection), and randomly generates a data packet of seq number to host B.
Second handshake: After receiving the request, host B needs to confirm the connection information and send ack number=(host A’s seq+1) to A. At the same time, syn=1, ack=1, and randomly generates a data packet with seq number and sends it to A
The third handshake: After receiving it, host A checks whether the ack number is correct, that is, the seq number + 1 sent for the first time, and whether the bit code ack is 1. If it is correct, host A will send another ack number = (host B's seq+1), ack=1. After receiving it, host B confirms the seq value and ack=1, then the connection is successfully established.
5.TCP transmission process
The above figure shows the process of host A transmitting 200 bytes to host B in 2 times (in 2 packets). First, host A sends 100 bytes of data through 1 data packet, and the Seq number of the data packet is set to 1200. In order to confirm this, host B sends an ACK packet to host A and sets the Ack number to 1301
In order to ensure that the data arrives accurately, the target machine must immediately send back an ACK packet after receiving the data packet (including SYN packet, FIN packet, ordinary data packet, etc.), so that the sender can confirm that the data transmission is successful.
At this time, the Ack number is 1301 instead of 1201, because the Ack number increases the number of bytes of transmitted data. Assume that the number of bytes transmitted is not added to each Ack number. Although the transmission of the data packet can be confirmed, it is not clear whether all 100 bytes are transmitted correctly or part of it is lost. For example, only 80 bytes are transmitted.
Therefore, confirm the Ack number according to the following formula: seq+number of bytes transferred+1
6. TCP flow control
The receiving end puts the buffer size it can receive into the "window size" field in the TCP header, and notifies the sending end through the ACK end; the larger the window size field, the higher the throughput of the network. Once the receiving end finds its own buffer When it is almost full, the window size will be set to a smaller value to notify the sender; after the sender receives this window, it will slow down its sending speed.
If the buffer at the receiving end is full, the window will be set to 0; at this time, the sender will no longer send data, but it needs to send a window detection data segment periodically to check whether the receiving end has a window and capacity to receive data.
7. TCP closing connection
The teardown of a TCP connection requires sending four packets, so it is called four waves.
Since TCP connections are full duplex, each direction must be closed individually
TCP’s four waves
(1) Client A sends a FIN to close the data transmission from client A to server B. (2) Server B receives this FIN, and it sends back an ACK, with the confirmation sequence number being the received sequence number plus 1.
(3) Server B closes the connection with client A and sends a FIN to client A. (4) Client A sends back an ACK message to confirm, and sets the confirmation sequence number to the received sequence number plus 1.
For more internet engineering knowledge and learning materials, check out the comments section!