当前位置:网站首页>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 .

原网站

版权声明
本文为[Straw hat learning programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021348159884.html

随机推荐