“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

A TCP.

1. Why does the TCP client send an acknowledgement at the end

In short, the main purpose is to prevent invalid connection request packets suddenly sent to the server, resulting in errors.

If you are using two handshake connection is established, suppose there is such a scenario, the client sends the first request connection and not lost, just because in the network node in the retention time is too long, as a result of the TCP client has not received the confirmation message, thought the server did not receive, at this time to send this message to the server, The client and server then complete the connection with two handshakes, transfer data, and close the connection. This message should be invalid. However, the two-handshake mechanism will allow the client and server to establish a connection again, which will lead to unnecessary errors and waste of resources. If the three-way handshake is used, even if the invalid packet is sent, the server receives the invalid packet and replies with an acknowledgement message, but the client does not send an acknowledgement again. Since the server does not receive an acknowledgement, it knows that the client has not requested a connection.

2. Why did you wave four times

In short, only the server knows if the data has been sent. When the data has been sent, the server tells the client that it is ready to shut down.

The client process sends a connection release packet and stops sending data. Release the header of the data packet, FIN=1, whose sequence number is SEq = U (equal to the sequence number of the last byte of the previously transmitted data plus 1). At this point, the client enters the fin-WaIT-1 state. According to TCP, FIN packets consume a sequence number even if they do not carry data.

After receiving the connection release packet, the server sends an acknowledgement packet with ACK=1, ACK= U +1 and its serial number seq= V. In this case, the server enters close-wait state. The TCP server notifies the higher-level application process that the client is released from the direction of the server. This state is half-closed, that is, the client has no data to send, but if the server sends data, the client still accepts it. This state also lasts for a period of time, i.e. the duration of the close-wait state.

After receiving the acknowledgement request from the server, the client enters the fin-WaIT-2 state and waits for the server to send a connection release packet (before receiving the final data from the server).

After sending the LAST data, the server sends a connection release packet with FIN=1 and ACK = U +1 to the client. The server is probably in the semi-closed state. Assume that the serial number is SEQ = W, then the server enters the last-ACK state and waits for the client’s confirmation.

After receiving the connection release packet from the server, the client sends ACK=1, ACK= W +1 and its serial number is SEq = U +1. In this case, the client enters the time-wait state. Note that the TCP connection is not released at this time, and the client can enter the CLOSED state only after 2 ∗ * ∗MSL (maximum packet segment life) and the corresponding TCB is revoked.

The server enters the CLOSED state immediately after receiving an acknowledgement from the client. Similarly, revoking the TCB terminates the TCP connection. As you can see, the server ends the TCP connection earlier than the client

3. What if the client suddenly fails after the connection is established

TCP also has a keepalive timer, so obviously if the client fails, the server can’t wait forever and waste resources. The server resets this timer every time it receives a request from the client, usually for two hours. If it does not receive any data from the client within two hours, the server sends a probe segment, which is then sent every 75 seconds. If there is no response after 10 probe packets are sent, the server assumes that the client is faulty and closes the connection

4. Slide window size

W = Bandwidth x latency

  • W Sliding window size
  • Bandwidth B 10 m/s
  • Round-trip time for delay data transmission TR TCPRTT
  • When W
  • When W>B*Tr, the factor that affects the rate is the bandwidth
  • TCP’s sliding window size is actually the number of bytes of the socket’s receive buffer size
/* * snd_size = 10*1024; /* Send buffer size is 8K */ optlen = sizeof(10*1024); err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, &snd_size, optlen); /* * Set the receive buffer size */ rcv_size = 10*1024; /* The receive buffer size is 8K */ opTLen = sizeof(rcv_size); err = setsockopt(s,SOL_SOCKET,SO_RCVBUF, (char *)&rcv_size, optlen);Copy the code

5. Sliding window principle

After receiving a reply ACK, the sending window moves the available window position and loads the data into the sending buffer

6. Queue head blocking problem

Head-of-line-blocking occurs when a TCP segment is lost, causing subsequent segments to reach the receiver out of order. The subsequent segment is retained by the receiver until the first lost segment is retransmitted by the sender and reaches the receiver. This subsequent sub-section of delayed delivery ensures that the receiving application process receives data in the order in which the sender sends it.

7. Fast failed retransmission and timeout retransmission

Timeout retransmission

  • Timeout retransmission time RTO is slightly larger than RTT, which is the average observation RTT
  • When timeout retransmission occurs, the RTO is doubled and the packet is sent again

The fast retransmission

  • Send data without waiting for ack to determine timeout
  • Instead, if more than three ACKS occur waiting for the same packet sequence, the failure is judged

As shown in the figure, ack =2 for three consecutive times triggers retransmission of the packet with SEq =2.

8. Unpacking

  • If the packet size is larger than the MSS size, you need to split the packet into multiple packets to send
  • If the sender sends multiple MSS packets at short intervals, the multiple packets received by the receiver need to be combined at the application layer. For example, the HTTP protocol uses Content-Length to indicate the packet size. Or by trun — packet — packet — 0.
  • Solution: Add a packet content size field to the package body by customizing the message

9. Quic contrast

10.ack

The ACK value is the current SEQ value + the packet size. When a packet is lost, the ACK value does not change until the packet is received. Each ACK contains the ID of the package expected to be received and the current window size. Just to make it easier to understand, we’ll call it package number 1. Given that the payload length of this packet is 100 bytes, it can be inferred that the next packet number should be 101

11.seq

  • To ensure the order and integrity of packets, TCP defines a seQ value for each packet.
  • The number of the first packet is a random number. Each packet is given two numbers: its own number and the number of the next packet. The recipient knows in what order they should be restored to the original file.

12. The TCP packet size

  • Ethernet packets are fixed in size, starting at 1518 bytes and later increasing to 1522 bytes. 1500 bytes are payload and 22 bytes are head information.
  • TCP packets need to reduce the IP packet header by 20 bytes and TCP packet header by 20 bytes. The overall maximum load is about 1460 bytes.

13. Size of TCP packets sent at one time

  • TCP The size of a TCP packet sent at a time is not a SINGLE TCP packet, but a group of TCP packets. The size of this set of TCP packets is controlled by a sliding window, which is directly related to the size of the receiving and sending Windows.

14. How does the application layer read data

  • Linux applications generally use sockets for I/O communication. The epoll mechanism can be used to add the handle of the socket to the listener for non-blocking data reading. Or you can just keep reading in an infinite loop.
Connfd = Accept (listenfd, (struct sockaddr*) &cliaddr, &addrSize); epoll_ctl(epollFd,EPOLL_CTL_ADD,connfd,&ev); While ((data= breader.readline ()))! =null){// Iterate over set client for(Socket s: client){// Print out sent data.write ((data+"\n").getBytes()); }}Copy the code
  • The application layer reads data according to the protocol, for example, HTTP reads the data size of the response according to content-Length.