TCP protocol:
1, the TCP protocol is defined as a reliable, but it is belong to the transport layer protocol, based on the definition of seven layer protocol and the data transport layer will pass the network layer (IP layer), and IP layer is try your best to do the delivery, here can understand into IP layer is not reliable protocol, then in IP layer on top of TCP protocol how reliable delivery? Here are a few ways to handle it:
-
Stopping the wait processing can be used as an example: A transmitting data via TCP protocol b, and in the TCP protocol definition, the data is sent need to be grouped, and of course the grouping situation is determined by the TCP protocol itself, and each sends a team of data to b requires b to send data to a confirmed successfully received the data, it will continue to send data to b, So what should A and B do if b fails to receive data or there are data errors due to network anomalies? B is here if you accept to the error information is directly discarded and does not return any data to inform a, if b can’t receive the data processing, don’t do a approach here is to send each data set a timer, if more than this time b has not been sent a confirmation message, a default send fail this time, then send the data to b again, Also known as “timeout retransmission” and “automatic retransmission request mechanism”, the timer is automatically turned off if a confirmation message is received from B. So here the timer time setting is very important, because this time to a little longer than normal transmission, if the short lead to multiple retransmission, waste of network resources, and side length, long will cause the efficiency and the normal transmission time is not good to determine success, because with after what network. So the question is, what does B do after receiving a retransmission? First b is to determine whether has the data first, if there is a will discard this data directly, if you don’t have continued to deliver data to the session layer and application layer, and no matter how, all want to send data and a confirmation again, because the reason lead to this situation is because have not received a confirmation. In this way, TCP provides reliable transport over unreliable transport at the IP layer. However, if data 2 is sent only after data 1 is sent successfully each time, data 1 and data 2 are in the same group, it seems inefficient. Then how to achieve high efficiency? Another mechanism used here is pipelined transmission, which means that the sender can send multiple data in parallel, rather than in serial, thus improving efficiency.
-
How to use TCP protocol to achieve flow control? Here the so-called flow control refers to let the sender send rate is not so fast, let the receiver timely processing, using the sliding window processing mode; For example, when a and B set up a connection (the three-way handshake), B will tell A how many bytes can be stored in its cache queue (RWND), and A will transfer data according to the value of RWND that B tells it. Each time B tells A, the first ACK value is 1, and the lowercase ACK value is the confirmation number. In this case, WHEN A transmits data, IT carries seQ, that is, the beginning of data transmission. If B tells it that RWND is 100, then the data transmitted by A is (1 ~ 100). When B tells A that RWND is 0, it proves that B does not allow the sender to send data again. If B can receive data again, it will send a value of RWND to tell A. Then the problem is, what if B’s network is not good during transmission and the value of RWND is lost? The solution is as follows: when A receives the zero window of B (RWND =0), A will fire a timer and send a zero window detector every fixed period of time, and B will give the current window value when receiving the zero window detection information, thus lifting the state of waiting for each other.
Tip: There is a problem here. The sliding window transfer is actually implemented for bytes. TCP transport is implemented in datagrams, so why is this implemented in bytes?
- More detailed three-way handshake process: Take sender A and receiver B as an example. As the receiver, B will first start the monitoring state to monitor whether the sender makes connection requests, which is called server here, and as the sender, which is called client here. The first handshake: If SYN=1 is set in the TCP header of A and SEq =x, the packet is sent to B. When SYN=1, no data is allowed, but the sequence number increases automatically. After this step is complete, the TCP status of A changes to SYN_SEND. Second handshake: When receiving data from A, B determines that there is a client connection according to SYN=1. In the TCP packet header, B sends ACK=1, ACK= x+1 and seq= Y to A for confirmation. At this moment, B’s state changes to SYN_RCVD. Third handshake: After receiving ACK=1 data, A will send ACK=1, ACK= Y +1, SQL = X +1 to B again, and then enter ESTABLISHED state. Data is allowed to be carried in this operation. If no data is carried, the serial number will not increase.
Tip: This raises the question, what happens if the third connection is removed? Here is an example to solve this problem, combined with more knowledge can know, if for the first time in a handshake when network delayed or lost problem, a protocol mechanism has a timer, timeout will send the request again, and it was sent twice a request connection operation, and due to network problems, One operation is received by B after the connection is completed. If the connection is completed after two handshakes, B will enter the Established state and wait for data transmission from A. However, A does not intend to transmit data to B, so B will wait and waste resources of B.
- Combined with the above knowledge points, let’s say that the process of four waves is more detailed: The following uses the TCP connection between A and B as an example. Before a disconnects the TCP connection with B, A sets the FIN signal in the header to 1 with the serial number seq= U, that is, the serial number +1 of the last transmission, and then sends the data packet. After that, IT enters the FIN-WaIT-1 state and waits for B’s confirmation. This is also the first handshake. After receiving the packet from A, B will judge that there is a client to wave connection according to FIN 1. B will set ACK signal to 1, ACK =u+1, and then send the packet, and then enter CLOSE_WAIT state. At this moment, the connection between A and B has been disconnected, that is, A has no data to send. The TCP connection is then entered into a half closed, because both TCP is connected, connection from a to b, but b to a connection is still there, and after a packet is received b into a state of the FIN – WAIT – 2, waiting for release the connection b, at the moment is complete the second wave, the third wave is b will send a FIN packet, After receiving the packet, A determines that it is the third wave based on the FIN and ACK = U +1. After receiving the packet, A sends an ACK packet and enters the TIME_WAIT state. It is important to note that the TCP connection is not closed, but waits for a timer before entering the CLOSE state. B will also enter the CLOSE state after receiving the data packet.
There are several problems that arise:
- A problem? Why does a wait for a timer to CLOSE on the fourth wave? Main reasons are: if a network is not good, when the fourth wave packets sent has been dropped, so b will due to not received the confirmation information retransmission packets, if can enter the CLOSE condition is a so sure not at the moment to receive b’s data, and make a wait for a timer time can solve this problem.
- Question 2? A shuts down when A releases the TCP connection in the second wave, so what state would B be in? And how does B handle this situation? If A is shut down, B will be in the last-ack state and cannot receive a’s confirmation packet when sending data to A. In order to prevent B from waiting indefinitely, B will start a keepalive timer mechanism and send a probe packet segment if b has not received the packet after two hours. If no response is received after 10 attempts, the connection will be disconnected.
- Question 3? A large number of CLOSE_WAIT states exist in server TCP connections. Why? If the connection from the close_wait state to the last_ACK state is connected to the last_ack state, the connection from the last_ACK state is not connected to the last_ACK state. If the connection from the last_ACK state is not connected to the last_ACK state, the connection from the last_ACK state is not connected to the last_ACK state. The possible CAUSE is I/O blocking.
2, TCP protocol used in the more efficient algorithm:
- Nagle algorithm: The purpose of Nagle algorithm is mainly used to solve a and B TCP protocol transmission of data transmission process, the process is: Application layer of the application will send data one by one byte transmitted to the transport layer of the TCP sending buffer, TCP first byte first sent to the receiving party, after the data to the cache, wait until the receiver response after the transfer, then send out the cached data, and then wait for the receiver response, continue to deliver the cache data to the sender, There’s a little bit of a serial approach here. Nagle also specifies that packets be sent as soon as they arrive at half the size of the send window. In this case, the first byte is sent to the receiver, and if the cache queue of the receiver is full after receiving this byte, then the cache queue of the receiver is full. The interactive application process reads bytes from the cache one by one. If the receiver sends an acknowledgement and tells the RWND is 1, and the sender sends a byte again after receiving the acknowledgement, the receiver processes a byte again, the efficiency will be very low in the long run. Then how to solve this problem? The TCP protocol is handled in this way. When the RWND is low, the receiver accumulates the packets and notifies the sender when the RWND is high. When the packets are low, the sender accumulates the packets and sends them when the packets are sufficient.