Finally, the TCP protocol of transport layer is summarized~~~

\u674e\u80b2\u6b22 2021-11-25 14:30:21

One . understand TCP( a key !!!)

Transmission control protocol , Is to carry out a detailed control of data transmission

1.TCP Form of agreement

 Insert picture description here

Two .TCP Security mechanism

1. Connection management mechanism ( important !!!)

1. The process of three handshakes

 Insert picture description here

  • host A send out syn To host B, Call for the establishment of A To B The connection of , Host status is syn_sent
  • host B reply ack+syn, Request to establish a host B To A The connection of , host B Status as syn_rcvd
  • host A Reply No 2 Step syn Of ack, host A Status as established, host B The status is set to estab

Be careful :
(1)syn Why are there two ?
Both parties will save the connection status ( There is a direction )
(2) The first 2 Step , Why ack+syn?
The essence is to send a ack The reply , One syn request , Two datagrams in the same direction , Can be combined
(3) The first 3 Step ,ack Confirm which ?
Answer No 2 Step by step syn

2. The process of four waves

 Insert picture description here

  • host A send out fin To host B, Request to close a To b The connection of
  • host B reply ack, host B The state is set to close_wait
  • host B send out fin To host A, Request to close B To A The connection of
  • host A Reply to the host ack( The first 3 Step by step ack) The state is set to time_wait, host B The datagram of step 4 is received , The state is set to closed

host A after 2MSL( Timeout waiting time ) after , The state is set to closed
Be careful :
(1) The first 2,3 Step , Why not merge ?
The first 2 Step is TCP The protocol is implemented in the system kernel in real time , Automatic response ack, The first 3 The next step is for the application to manually call close To close the connection , Before closing the connection , You may need to perform pre operations such as releasing resources , So you can't merge (TCP When the protocol is implemented , No such design )
(2) The first 3 Step , host A Why can't it be set directly to closed state
The first 4 A datagram may lose packets : host B After the time-out , Resend page 3 A datagram , Will ask the host A Reply again ack
(3) There are a lot of close_wait state , Why ? How to solve ?
The server does not close the connection correctly ( The program did not call close, Or not called correctly )

2. Confirmation response mechanism

1. principle

  • Keep it safe : The message I sent , The other party must confirm and reply
  • Ensure the security of multiple data confirmation messages ( Serial number + Confirm the serial number )

2. Realization

  • When sending : Carry data serial number
  • When confirming the reply : Carry the confirmation serial number

3. Timeout retransmission mechanism

1. principle

If the host A The host is not received within a specific time interval B The confirmation from , Will be retransmitted

2. No confirmation response is received

  • The datagram is lost
  • ack Confirm that the response datagram is lost

3. How to determine the timeout

According to the current network environment ( Determine the speed at which data is sent ), Get the maximum lifetime of a single message (MSL), The timeout is 2MSL
Be careful :TCP In order to ensure high performance communication in any environment , Therefore, the maximum timeout will be calculated dynamically

4. I can't receive it all the time ack, How will the timeout be set

  • In most operating systems , Timeout to 500ms Control for a unit , The time-out of retransmission is determined every time 500ms Integer multiple
  • If after a resend , Still no response , wait for 2*500ms Retransmission after
  • If you still don't get an answer , wait for 4*500ms Retransmission , And so on , Increasing in the form of qualitative index
  • Accumulated to a certain number of retransmissions ,TCP Think that the network or the opposite host is abnormal , Forcibly close the connection

4. Flow control mechanism

  • The receiving capacity of the receiving end is limited , Tell the sender about the receiving capacity
  • Flow control window : be based on tcp Set the window size field in the message ( Affects the size of the sliding window at the sending end )

5. congestion control mechanism

1. summary :

The sender's network status is unknown , Send a lot of datagrams rashly , It will cause network congestion , First send data Pathfinder , Set congestion window size )

2. How to determine the size of congestion window ?

 Insert picture description here

  • Slow start : Send only a small amount of data at first , Detect the degree of network congestion , Then gradually increase the transmitted data , The congestion window will grow exponentially ;
  • Congestion avoidance : When the congestion window reaches the threshold of slow start , The congestion algorithm is used to make the congestion window increase slowly , No exponential increase , But the addition increases ( After each round trip, the window size is increased by 1), In this way, the congestion window will increase slowly according to the linear law ;
  • Fast retransmission and fast recovery : Fast recovery of lost packets ;
  • TCP When it starts up , The threshold for slow start is equal to the maximum value of the window ; When the time-out retransmission , The threshold for slow start becomes half the maximum congestion window , At the same time, the congestion window is reset 1;

3、 ... and .TCP Efficiency mechanism

1. Delay response mechanism

The receiving end receives data , Respond immediately ack, Flow control window size ( Free space of receiving buffer at receiving end ) It will be smaller
Affect sliding window size ( The greater the window , The more efficient , The smaller the window, the less efficient )
Solutions : Wait a while , Let the receiver program receive and process the buffer data, and then return ack

2. Piggyback response mechanism

The receiver responds ack, And actively sent data , You can merge and return

3. The sliding window

1. Why have sliding windows

If there is no sliding window , Network data transmission is a serial way , The efficiency is poor , Using sliding windows can solve the problem of efficiency , Similar to multithreading : concurrent , Send multiple datagrams at the same time
 Insert picture description here

2. Implementation process :( Here, the window size is 4000 Bytes ( Four sections ) Illustrate with examples ):

 Insert picture description here

  • Window size refers to the maximum value that can continue to send data without waiting for an acknowledgement , The sliding window above is 4000 Bytes ( Four sections )
  • When sending the first four segments , There's no need to wait for any ack, Direct transmission
  • Got the first one ack after , Slide the window back , Continue sending the second 5 Segment data , By analogy
  • The operating system kernel maintains this sliding window , You need to open up a send buffer to record which data is not answered , Only confirm the data that has been answered , To delete from the buffer
  • The greater the window , The higher the throughput of the network

3. You must know about sliding windows :

  • Window size : The maximum value at which data can continue to be sent without waiting for an acknowledgement
  • How to determine the window size : Determined by congestion window and flow control window , That is, the sliding window size =min( Congestion window size , Flow control exposure size )
  • How to slide : rely on ack The confirmation number of (ack Confirm that the datagrams before the serial number have been received ), In the ack Before confirming the serial number , How many datagrams are received in parallel , How much you can slide
  • Why should the sender have a send buffer : Record the data that has been sent , How many ack, To clean up the data
  • Why should the receiving end have a receiving buffer : Record the received data , If the transmission data is lost , Just know to let the other party resend

Four .UDP agreement

There's no guarantee of safety , Good performance

1.UDP Features of the protocol

  • There is no connection : Know the right side IP And the port number are transmitted directly , No connection needed
  • unreliable , That is, there is no confirmation response mechanism , There is no timeout retransmission mechanism , There is no connection management mechanism , If the segment cannot be sent to the other party due to network failure ,UDP The protocol layer will not return any error information to the application layer
  • For datagram , No more than 64K, Can not flexibly control the number and number of reading and writing data , Application layer to UDP How many messages ,UDP The same to send , Will not split , Will not merge
  • With receive buffer , No send buffer

2.UDP Precautions for use of

UDP The first part of the agreement has a 16 The maximum length of bits , That is to say a UDP The maximum length of data that can be transmitted is 64K( contain UDP The first one ), However 64K In today's Internet Environment , It's a very small number , If we need to transmit more data than 64K, You need to subcontract manually at the application layer , Send... Multiple times , And manually assemble at the receiving end

5、 ... and .TCP agreement VS UDP agreement

1.TCP agreement

  • Transport layer protocol
  • There is a connection
  • Reliable transmission
  • Byte stream oriented

2.UDP agreement

  • Transport layer protocol
  • There is no connection
  • Unreliable transmission
  • For datagram , No more than 64K

3. Scene analysis

  • TCP For reliable transmission , Apply to file transfer , Important status update and other scenarios
  • UDP It is used in the communication field with high requirements for high-speed transmission and real-time performance , for example : Early rising QQ、 Video transmission, etc , in addition UDP Can be used to broadcast

To make a long story short ,TCP and UDP They are all tools of program apes , When to use , How to use it , It should be determined according to the specific demand scenario

4.TCP And UDP The difference between

  • TCP It's connection-oriented ,UDP There is no connection ;
  • TCP Is reliable ,UDP unreliable ;TCP Only point-to-point communication is supported ;
  • UDP Support one-to-one 、 One to many 、 For one more 、 Many to many communication ;
  • TCP Is oriented to a byte stream ;UDP It's message oriented ;
  • TCP The first one costs a lot ,20 Bytes ; UDP Only 8 Bytes ;
  • TCP It can ensure the data sequence of transmission , UDP There is no guarantee ;

6、 ... and .UDP How to achieve reliable

If you use UDP To achieve reliable , Then we must solve two problems : Packet loss and packet order ; Implement in the application layer

  1. Number packets , Receive and store in the order of packets ;
  2. After the receiving end receives the data packet , Send a confirmation message to the sender , After receiving the confirmation data, the sender will continue to send the next packet ; If the packet number received by the receiving end is not the expected number , Then the sender is required to resend

It's done UDP A protocol for reliable transmission :

  1. RUDP: Reliable datagram transmission protocol
  2. RTP: Real time transmission protocol , It provides real-time end-to-end transmission service for data ;
  3. UDT: Internet transmission protocol , The main purpose is to support massive data transmission over high-speed Wan ;

7、 ... and .TCP Possible problems — Stick package problem

Based on the transport layer TCP To implement the application layer protocol , because TCP It's based on a byte stream , When reading, consider the format , Including reading , If the format is not read correctly , Or the length is not set , There will be sticking problems
How to solve :
Define the boundary of the package

Please bring the original link to reprint ,thank
Similar articles