Big front-end encyclopedia, front-end encyclopedia, record the front-end related field knowledge, convenient follow-up access and interview preparation

keywords
  • Three handshakes and four waves
  • The sliding window
  • Error control
  • Flow control
  • Congestion control
  • Talk about the use of sliding Windows in network communication

The transport layer

  • TCP(Transimision Control Protocal)

    • Transmission control protocol
    • A reliable, connection-oriented protocol
    • Low transmission efficiency
  • UDP(User Datagram Protocal)

    • User datagram protocol
    • Unreliable, connectionless services
    • High transmission efficiency

TCP

The transport layer is a connection-oriented, reliable process-to-process communication protocol. TCP provides the full-duplex service, that is, data can be transmitted in both directions at the same time. TCP combines several bytes into a group, which is called a Segment. Provides an end-to-end connection. The transport layer protocols are TCP and UDP. TCP(TCP Control Protocal) is a reliable and connection-oriented protocol with low transmission efficiency.

TCP functions
  • The data is segmented and packaged for transmission
  • Control the sequence of each packet number
  • Lost, reissued and discarded in transit
  • Flow control to avoid congestion
TCP Packet Encapsulation

  • Source port number and destination port numberThe computer identifies which service to access by port number, such as HTTP service or FTP service. The sender port number is the random port. The target port number determines which program to receive
  • 32-bit serial numberTCP marks packets with serial numbers so that they can be reassembled after arriving at the destination. Assuming that the current serial number is S and the length of the sent data is L, the sequence number of the next data is S + L. When establishing a connection, the computer usually generates a random number as the initial value of the serial number
  • Confirm answer numberIt is equal to the serial number of the data that should be received next time. If the serial number of the sender is S and the length of the data sent is L, the acknowledgement number returned by the receiver is also S + L. When the sender receives this acknowledgement, it can be assumed that all previous data at this location has been received normally.
  • The length of the first: Indicates the length of the TCP header, in 4 bytes. If there are no optional fields, then the value here is 5. Indicates that the length of the TCP header is 20 bytes.
  • Control bitsTCP connections, transmissions, and disconnections are directed by these six control bits
    • PSH(Push urgency bit) cache will be full, transfer speed immediately
    • RST(reset reset bit) The connection is disconnected and the connection is reconnected
    • URG(Urgent Urgent bit) Indicates an emergency signal. Data is sent immediately
    • An ACK(Acknowledgement) value of 1 indicates the acknowledgement number
    • SYN(synchronous connection) Serial number Set this value to 1 when TCP connections are established
    • The FIN sender completes the bit. The FIN sender sets the value to 1 to indicate that the connection is to be disconnected
  • Window valueIndicates the number of data segments that can be received locally. This value is variable in size. When the network is smooth, increase the value of this window to speed up the transmission speed; when the network is unstable, reduce the value of this window to ensure the reliable transmission of network data. It is used for flow control over TCP transmissions
  • The window size: Indicates how many 8-bit bytes can be accepted starting with the answer number. If the window size is 0, window probes can be sent. (Set window size based on network bandwidth + receiver processing speed)
  • The efficacy and: Used for error control, the CALCULATION of the TCP checksum includes the TCP header, data, and other padding bytes. The checksum is calculated by the sender when the TCP data segment is sent, and is checked and calculated again when the destination is reached. If the two checksums are the same, the data is correct. Otherwise, the data is considered corrupted and the receiver discards the data
  • Pointer to an emergency: This parameter is valid only when the URG control bit is 1. Represents the position of the end of the emergency data in the TCP data section. Usually used to temporarily break communication (e.g. Ctrl + C).
Error control

The checksum is used for error control. The CALCULATION of the TCP checksum includes the TCP header, data, and other padding bytes. The checksum is calculated by the sender when the TCP data segment is sent, and is checked and calculated again when the destination is reached. If the two checksums are the same, the data is correct. Otherwise, the data is considered corrupted and the receiver discards the data

Three-way handshake

TCP is connection-oriented. Before either party sends data to the other, a connection must be established between the two parties. In TCP/IP, TCP provides a reliable connection service that is initialized with a three-way handshake. The purpose of the three-way handshake is to synchronize the serial number and confirmation number of the two sides of the connection and exchange TCP window size information.

  • First handshake: Establish a connection. The client sends a connection request, sends a SYN packet, and sets seQ to 0. The client then enters the SYN_SEND state and waits for confirmation from the server.
  • Second handshake: The server receives a SYN packet from the client. The SYN segment needs to be acknowledged, and an ACK packet needs to be sent with ACK set to 1. It also sends a SYN request, setting seq to 0. The server sends all the above information to the client, and the server enters the SYN_RECV state.
  • Third handshake: After receiving ACK and SYN packets from the server, the client acknowledges them, sets ACK to 1 and SEQ to 1, and sends an ACK packet to the server. After the ACK packet is sent, the client and server enter the ESTABLISHED state to complete the TCP three-way handshake.

The data transfer

  • The client first sends data to the server in a datagram of 159 bytes.
  • After receiving the packet, the server also sends a data acknowledgement (ACK) to the client and returns the data requested by the client. The length of the data is 111, the SEQ is set to 1, and the ACK is set to 160 (1 + 159).
  • The client acknowledges (ACK) upon receiving the data returned from the server, setting seQ to 160 and ACK to 112 (1 + 111).

Four times to wave

  • First wave: The client sends a FIN packet segment to the server. The SEQ and ACK are set to 160 and 112 respectively. At this point, the client enters the FIN_WAIT_1 state, which means that the client has no data to send to the server and requests to close the connection.
  • Second wave: The server receives the FIN packet from the client and sends an ACK packet with ACK set to 1 and SEQ set to 112 to the client. The server enters the CLOSE_WAIT state. After receiving the ACK packet from the server, the client enters the FIN_WAIT_2 state.
  • Third wave:The server watches to see if it has any data it hasn't sent to the clientIf yes, data is sent to the client first and then FIN packets are sent. If no, the server directly sends FIN packets to the client. The server is requested to close the connection and enter the LAST_ACK state.
  • Fourth wave: The client receives the FIN packet from the server, sends an ACK packet to the server, sets seQ to 161, ACK to 113, and enters the TIME_WAIT state. After receiving the ACK packet from the client, the server closes the connection.At this point, the client waits for 2MSL and still does not receive a reply, it proves that the Server has been shut down normally, the client can also close the connection.

Note: when shaking hands and waving hands, the confirmation number should be the serial number of the other party plus 1. When transmitting data, it should be the serial number of the other party plus the length of the application layer data carried by the other party.

Issues related to
  1. Why three handshakes? Make sure both parties are sending and receiving normally
  2. Why do you need four waves? Ensure that both parties have finished sending data and agree to disconnect
  3. Why wait? The FIN sent from A to B may be lost
  4. Why shake hands three times, but wave hands four times? After receiving a FIN packet, the Server may not close the SOCKET immediately

udp

  • UDP is a connectionless transport layer protocol that does not ensure reliability. That is, the sender does not care whether the sent data reaches the destination host or whether data errors occur, and the receiving host does not tell the sender whether the data has been received. Its reliability is guaranteed by upper-layer protocols
  • The header has a simple structure and can achieve minimum overhead in data transmission. It can be used if the process wants to send very short messages and does not have high reliability requirements

The sliding window

  • Sliding window is a flow control technique
  • In the early days of network communication, communication parties directly sent data regardless of network congestion. Because everyone did not know the network congestion, at the same time to send data, resulting in the middle node blocking switch, no one can send data, so there is a sliding window mechanism to solve this problem
  • TCP uses sliding Windows for transmission control,The size of the sliding window means how much buffer the receiver has available for receiving data. The sender can use the size of the sliding window to determine how many bytes of data to send
  • When the sliding window is 0, the sender can no longer send datagrams, except in two cases where emergency data (URG) can be sent, for example, allowing the user to terminate a running process on the remote machine. Alternatively, the sender can send a 1-byte datagram to inform the receiver to redeclare the next byte it wishes to receive and the size of the sender’s sliding window
Window mechanism
  • The basic principle of sliding window protocol is that at any time, the sender maintains a continuous sequence number of frames allowed to be sent, which is called the sending window. At the same time, the receiver also maintains a serial number of frames that are allowed to receive, called the receive window
  • The upper and lower bounds of the serial number of the sending and receiving Windows do not have to be the same, and even the size can be different
  • Different sliding window protocols generally have different window sizes
  • The serial number in the sender window represents frames that have been sent, but have not yet been confirmed, or are ready to be sent

Congestion control
  • TCP congestion Control is an algorithm of Transmission Control Protocol (TCP) to avoid network congestion. It is a major congestion Control measure on the Internet
  • TCP uses a variety of congestion control strategies to avoid avalanche congestion. TCP maintains one for each connectionCongestion windowTo limit the total number of unacknowledged packets that may be transmitted end-to-end
  • This is similar to the sliding window used in TCP flow control and is controlled by the sender
  • TCP uses one after a connection has been initialized or timed outSlow startMechanism to increase the size of the congestion window. The starting value is usually twice the Maximum segment size (MSS).Although it is called slow start and the initial value is quite low, it grows very fast: As each segment is confirmed, the congestion window will add an MSS, making each segment more efficientRound-trip time (RTT)The internal congestion window can efficiently double
  • In flow control, the receiver informs the sender through the TCP Window Size, and the sender determines the amount of data to be transmitted at any time by comparing the Size of the congestion Window and the receiving Window
  • The sum goes up, the product goes down(Additive -increase/multiplicative- Decrease, AIMD) is a feedback control algorithm, which includes linear increase of the congestion window and linear reduction of the window product when congestion occurs. Multiple TCP streams using AIMD control eventually converge to equal competing use of the line.
  • Unacknowledged packets are exactly equal to bandwidth equals delay
  • Cut the bag in half as soon as it's missing

Talk about the use of sliding Windows in network communication

  • The sliding window
  • Flow control
  • Congestion control

role

What we do know is that the network layer is actually a datagram network, which itself is not connection-oriented and does not provide reliable, ordered and complete services. UDP uses the datagram network directly, so it just provides the best service possible, and packets can be lost in transit. So why is IT that TCP, which also uses a datagram network, is able to implement a reliable connection-oriented transport service? In fact, because the reliable transmission of TCP is not dependent on the lower layer of the network layer to complete, completely in the transmission layer, completed in the software level, specifically reflected in the TCP protocol has received confirmation, there is a timeout retransmission, repeat confirmation and other additional steps.

Because sending and receiving datagrams have these limitations compared to UDP, it is possible to implement the service of reliable transmission. These additional steps also bring some problems: More fields need to be set in the header field to complete those steps. The extra steps will cost time and resources. The most important fields in the header field are ACK, byte sequence number, and SEP, which guarantee the correct retransmission of lost packets

However, the efficiency of sending a packet, waiting for the acknowledgement, and then sending the next packet is very low.

With the sliding window, the communication parties do not need to send a message and then send the next message after receiving the confirmation of the message, but can send multiple messages as long as they do not exceed the window size limit

TCP has a higher cost than UDP. Once network congestion or packet loss causes packet retransmission, which aggravates the congestion. Therefore, TCP strictly controls the transmission rate to prevent network congestion

Conclusion:

  • The sliding window allows the sender to send multiple packets consecutively
  • The sending frequency of the sender is limited according to the size of the receiving window of the other party to prevent congestion

Note that in practice, the sending frequency is not determined by the RWND size of the receiving window, because network congestion is not considered. In congestion control, the size of CWND of the sending window will also be determined, and the MIN value will be taken at the end of sending

What is the mechanism of sliding Windows (network communication)?

TCP is full-duplex communication, so the sliding window of each side includes the receiving window and the sending window. The receiving window is responsible for processing the received data, and the sending window is responsible for processing the data to be sent.

In fact, the essence of sliding window is to maintain several variables, through which the data processed by TCP is divided into several categories. At the same time, a packet is sent and a packet is received to do certain processing and maintenance for these variables

Analysis send window

  • N is the start byte of the send window, that is, bytes < N have been sent and received an ACK
  • NextSeq is the first Seq field (Seq is the sequence number of the first byte) of the next packet to be sent. It indicates that all bytes in the range [N, nextSeq] have been used and sent, but no ACK has been received
  • N+size is the last available byte number of the window, size is the size of the sending window, is the value of the Win field of each received message, Win field is actually the size of the receiving window of the other party

How do you maintain these values?

  • When receiving a packet, perform the following operations: Check the ACK of the received packet and set N to ACK, that is, move forward to ack. Read the win field value in the packet, that is, the size of the latest receiving window of the other party, and update the value of N+size
  • Every time a packet is sent, the value of nextSeq is changed, and the nextSeq is moved forward by as many bytes as it is sent, but not by more than N+size

Analysis receiving window

It also maintains several variables about the byte ordinal, similar to the sending window, but the byte ordinal of the receiving window is the received byte. Meaning of several variables:

  • J1 indicates that bytes < J1 have been received, that is, an ACK has been sent, that is, the program is ready to use
  • J2 indicates that the bytes in [J1,J2] have been received in order and are ready to be read by the program, but it has not yet sent an ACK for confirmation
  • J3 is the last received byte of the receive window, as calculated by J1+ the size of the receive window. Messages with bytes larger than J3 are rejected (and generally not received, since the size of the sending window is determined by the receiving window and cannot exceed).

How do you maintain it?

  • When sending a message: fill the value of J2 into the ACK field in the header of the message, and then move J1 to J2 to tell the other party that the bytes you sent with serial number less than J2 have been correctly received and used; Insert the interpolation of (j3-j2) into the first Win field to tell the other side how much space I have left to receive
  • When a message is received, if the order of the messages is correct, the number of bytes will be moved. If the packets arrive in sequence, the Seq value of the next received packet should be equal to J2. If the Seq value is not equal, J2 is not moved. Therefore, the next ack packet is always the same: J2, that is, repeated acknowledgement, and corresponding fast retransmission

conclusion

Since the starting value of a window gradually increases after it starts, that is, moves to the right, this is how sliding Windows get their name. In fact, it maintains several variable values about ACK, byte number, and SEQ (packet start byte number) when receiving and sending packets. Summary about ACK, SEQ and WinSize

  • How to send the ack and how to receive the ack:
When sending a packet, fill the value of J2 in the ack of the packet header from the receiving window. After receiving an ACK, the start value N in the sending window is updated to an ACK.Copy the code
  • How does seQ come from sending a message and how does seQ come from receiving a message?
When sending a packet, obtain the value of nextSeq from the sending window and fill it into the SEQ field in the packet header. After receiving the packet, check whether the SEQ field of the packet is J2 in the receiving window. That is, J2 is continuously moved to the right according to the packet lengthCopy the code
  • How does Win come from sending the message and how does Win come from receiving the message?
When sending a message, the difference value (j3-J2) obtained from the receiving window is filled into the win field at the head; The value of the Win field in the header received when the packet is received is assumed to be size, so the end position of the update sending window is N+sizeCopy the code