当前位置:网站首页>Do you really know tcp/ip
Do you really know tcp/ip
2022-06-09 08:17:00 【Straw hat learning programming】
background :
Enter from us IT The industry has been in constant contact with TCP/IP agreement . But most people are right most of the time TCP/IP The agreement is well known , Just plain know TCP Link based reliable transport protocol , There are three handshakes and four waves , And then it's gone . This article will add a little bit of this knowledge .
One 、 A network model
The network model can be roughly divided into :TCP/IP Models and OS Model . Here's the picture

You can find ,TCP/IP A model is actually a model that will OS The model is divided more generally . It is mainly divided into the following four layers :
- application layer : Layers of communication between applications , Such as simple e-mail transmission (SMTP)、 File transfer protocol (FTP)、 Network remote access protocol (Telnet) And us Android Use the most Http Agreements, etc .
- Transport layer : It provides data transmission service between nodes , Such as transmission control protocol (TCP)、 User datagram protocol (UDP) etc. ,TCP and UDP Add transfer data to the packet and transfer it to the next layer , This layer is responsible for transmitting data , And make sure that the data has been delivered and received .
- Network interconnection layer : Responsible for providing basic data packet transmission function , Let every packet reach its destination The host ( But don't check if it's received correctly ), Such as internet protocol (IP).
- Host to network layer : The management of the actual network media , Define how to use the actual network ( Such as Ethernet、Serial Line etc. ) To transmit data .
Here we only need to remember the four layers, the meaning of each layer and the corresponding protocol .
Two 、TCP agreement
First, let's take a look at how packets work in TCP/IP Transmitted in the protocol .

From the above figure, we can clearly see that there will be a packet or unpacking process when data packets enter each layer . When the data arrives at the transport layer, it goes through TCP What is the structure of the data packet after the protocol packet ?

In this structure, we need to focus on the serial number and window size , Because this will be the knowledge point involved in the next content .
2.1、 Three handshakes
Since it means TCP agreement , It is inevitable to mention its famous three handshakes

Pictured ,TCP Three handshakes of technological process :
- Client send out SYN=1 and seq=x Packet to Server Back entry SYN_SEND state , Wait for the server to confirm , there seq It's randomly generated Serial number .
- Server Received SYN After the packet , Confirm and return SYN+ACK+ack+seq Data packets , Then enter SYN_received state . there ack= Client's seq+1. And here it is seq It is randomly generated by the server Serial number .
- Client Received from the server SYN+ACK After the packet , Confirm and return ACK+seq+ack, Then enter Established state , The server receives ACK After the data packet, it also enters Established state . At this point, both parties inform the upper layer that the connection is successfully established , You can send and receive application data . Actually, the third handshake Client You can send data to the opposite end .
But I'm not talking about the process of three handshakes , Here's what I'm going to say Why three handshakes , Not twice or four times :
- TCP Is a reliable link based transport protocol . Three handshakes are used to confirm the link , Guarantee TCP Link reliability .
- And guarantee TCP The core of reliable link is to exchange the initial serial numbers of both parties in the process of handshake , However, each time we send data, it is not immediately sent ,socket There will be a buffer and sort operation , Send again when the buffer queue is full . Each segment of data sent contains a serial number , After receiving the data packet, the server and the client verify the sequence number and confirm the response to ensure the reliability of the data and the reliability of the link .( The retransmission mechanism is based on this implementation , If the confirmation response cannot be received, it will be retransmitted ) The initial value of this serial number is exchanged through three handshakes . Although two handshakes can exchange initial serial numbers , But the serial number exchange on the server does not confirm , And four handshakes are superfluous , So three handshakes are the most efficient , And be able to complete the task . Here's another one tcpNoDelay Algorithm , If enabled, the next packet will not be sent until the acknowledgement of the previous packet is received .

It is said that three handshakes are reliable , Then it has no loopholes ? Of course. :
- SYN Flooding attack : By sending a large number of attack messages that forge the original address to the port where the server is located , The half open link queue of the server is full , This prevents other users from accessing .
Solutions to attacks : Invalid link monitoring release 、 delay TCP Distribution method 、 A firewall
2.2、 Four waves
Since there is a handshake , There must be a time to say goodbye . Let's see what we did with the four waves :

Pictured , Four waves is to send to the other side FIN message , And confirm with each other . But why don't you wave your hand four times instead of three times like shaking hands ( Why four waves ):
- TCP After three handshakes to establish the link, there is a full duplex communication , When received from one party FIN When sending a message, it only means that the other party will no longer send data , But still able to receive data , To disconnect completely , Both parties need to send FIN Message and mutual confirmation .
Why wait after sending the confirmation packet for the last time of four waves 2MSL( Two maximum message generation times ):
- This is because if the last sending confirmation message is lost, etc , As a result, the peer fails to receive the confirmation packet , So the opposite side is waiting here 2MSL It will be sent again in time FIN Message and wait for confirmation . Doing so can greatly ensure that the link is broken properly , Avoid port conflicts .
2.3、 Sliding window mechanism
This question , Not many . But the interviewer didn't know it was to pretend 13 Or for what , Anyway, I like to ask . So let's have a brief look
What is a sliding window :
- Both the sender and the receiver maintain a sequence of data frames , This sequence is called window .
- The sender's window size is confirmed by the receiver .

What can a sliding window do :
- Make sure the data is not lost : If the data sent is lost, it can be sent again ( This is what we often call the retransmission mechanism ).
- Control the sending speed : Control the sending speed , In order to avoid overflow due to insufficient cache size of the receiver , At the same time, controlling traffic can also avoid network congestion .
边栏推荐
- C language review 10
- Specific steps for yolov5 to add attention mechanism
- GC学习笔记
- Elk+filebeat deployment and installation
- 【学校实验+蓝桥杯题目】接水问题:学校里有一个水房,水房里一共装有m个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为1。现在有n名同学准备接水,他们的初始接水顺序已经确定......
- Clickhouse distributed table & local table & Clickhouse realize time series data management and mining
- An integrated framework for caching, loading, refreshing and mapping data dictionaries
- Twitter的最新功能让商家预告即将推出的产品 提醒顾客去购物
- R language through rprofile Site file, custom configuration of R language development environment startup parameters, shutdown parameters, use file Edit function edit configuration file
- Alibaba Cloud ACK Access Enterprise version ACR Mirror
猜你喜欢
![[penetration test] summary of conditional competition vulnerabilities](/img/8b/d365d7c1f16fa1be392770209c6454.jpg)
[penetration test] summary of conditional competition vulnerabilities

SQL or NoSQL, you will understand after reading this article

redis核心知識點總結(超詳細)

【读点论文】Benchmark Analysis of Representative Deep Neural Network Architectures当准确率不再是唯一衡量标准,给多目标优化一个导向

RedLock红锁安全性争论(上)

C language review 12
![Nacos 启动报错[db-load-error]load jdbc.properties error](/img/e0/e511da6cd6821ffb315d118a2feae9.png)
Nacos 启动报错[db-load-error]load jdbc.properties error

自制编译器学习4:使用Flex

MySQL common interview knowledge points

JVM体系架构学习笔记
随机推荐
Research and investment strategy report on China's FRP membrane shell industry (2022 Edition)
Market Research - current situation and future development trend of global and Chinese cosmetic grade glycyrrhetinic acid Market
An integrated framework for caching, loading, refreshing and mapping data dictionaries
Real time monitoring, intelligent early warning, CDC's "speed" of war and epidemic
Market Research - current situation and future development trend of global and Chinese wall mounted extraction arm Market
XML JSON yaml conversion
Understand the difference between left join, right join and join
Quarkus实战学习一
Nacos 启动报错[db-load-error]load jdbc.properties error
自制编译器学习1:Cb编译器的使用
Elk+filebeat deployment and installation
Kibana: introduction to kibana (I)
Clickhouse distributed table & local table & Clickhouse realize time series data management and mining
Market Research - current market situation and future development trend of cosmetic grade ethylhexyl glycerol in the world and China
Market Research - current situation and future development trend of global and Chinese dental zirconia materials market
At time_ What happens to TCP connections in wait status after SYN is received?
【读点论文】Benchmark Analysis of Representative Deep Neural Network Architectures当准确率不再是唯一衡量标准,给多目标优化一个导向
[reading point paper] ghostnet: more features from cheapoperations convolution operation is still expensive, and feature graph redundancy can be obtained by linear transformation
Graphic knowledge points: leakcanary
Alibaba cloud ack pull free enterprise ACR image