Computer Network-TCP/IP HTTP Conclusion

1.1 The structure of OSI and TCP/IP layers

1.2 Three-way handshake and four-way wave, why TCP three-way handshake and four-way wave

In the first message sending, A randomly selects a sequence number as its initial sequence number and sends it to B; the second message B uses ack to confirm A's data packet,

Because the data packet with the sequence number x has been received, it is ready to receive the packet with the sequence number x+1, so ack=x+1, and B tells A its own initial sequence number, which is seq=y;

The third message A tells B that it has received the confirmation message from B and is ready to establish a connection. A’s own sequence number of this message is x+1, so seq=x+1, and ack=y+1 means that A is preparing to receive B data packet with sequence number y+1

1.2.1

Why does the Client need to perform a third "handshake" after receiving the confirmation from the Server?

The three-way handshake is used to prevent the invalid connection request segment from being suddenly transmitted to the host B, resulting in an error.


The "invalid connection request segment" is generated in such a situation: the first connection request segment sent by the client is not lost, but stays at a network node for a long time (because the network A large amount of concurrency is blocked at a certain node
), so that the server is delayed until a certain time after the connection is released. Originally this was a long-defunct segment. However, after the server receives the invalid connection request segment, it mistakenly thinks that it is a new connection request sent by the client again. So it sends a confirmation segment to the client and agrees to establish a connection. Assuming that the "three-way handshake" is not used, a new connection will be established as long as the server sends an acknowledgment. Since the client has not issued a request to establish a connection now, it will ignore the confirmation of the server and will not send data to the server. But the server thinks that a new transport connection has been established, and has been waiting for the client to send data. In this way, many resources of the server are wasted in vain. The "three-way handshake" approach can prevent the above phenomenon from happening. For example, in the case just now, the client will not issue an acknowledgment to the server's acknowledgment. Since the server cannot receive the confirmation, it knows that the client has not requested to establish a connection. ”. The main purpose is to prevent the server from waiting all the time and wasting resources .

1.1.3

Why wave 4 times?

Make sure the data can complete the transfer. But when closing the connection, when receiving the other party's FIN message notification, it just means that the other party has no data to send to you; but not all your data is sent to the other party, so you may not close SOCKET immediately, That is, you may also need to send some data to the other party, and then send a FIN message to the other party to indicate that you agree that the connection can be closed now, so the ACK message and the FIN message here are sent separately in most cases. The TCP protocol is a connection-oriented, reliable, byte stream-based transport layer communication protocol. TCP is full-duplex mode, which means that when host 1 sends a FIN segment, it just means that host 1 has no data to send, and host 1 tells host 2 that all its data has been sent; however, At this time, host 1 can still accept data from host 2; when host 2 returns an ACK segment, it means that it already knows that host 1 has no data to send, but host 2 can still send data to host 1; when host 2 also When the FIN segment is sent, it means that host 2 has no data to send, and will tell host 1 that I have no data to send, and then they will happily terminate the TCP connection.

 1.1.4

How to eliminate TIME_WAIT caused by a large number of short TCP connections?

It can be changed to a long connection, but the cost is high. Too many long connections will cause server performance problems

 Modifying ipv4.ip_local_port_range to increase the range of available ports can only alleviate the problem, not fundamentally solve the problem

1.1.5

What if the last ACK is lost when closing the connection?

If the last ACK is lost, TCP will consider its FIN lost and resend the FIN. After the client receives the FIN, it will set a 2MSL timer. The 2MSL timer can make the client wait long enough to wait for the next FIN if the ACK is lost. If a new FIN arrives in the TIME-WAIT state summary, the client sends a new ACK and resets the 2MSL timer.

2.1

How does TCP guarantee reliable transmission? (emphasis!!!)

0. Before transferring data, there will be a three-way handshake to establish a connection.

1. The application data is divided into data blocks that TCP thinks are most suitable for sending (numbered by bytes, and reasonably fragmented). This is completely different from UDP, and the length of the datagram generated by the application will remain the same. ( Truncate the data to a reasonable length )
2. When TCP sends a segment, it starts a timer and waits for the destination to acknowledge receipt of the segment. If an acknowledgment cannot be received in time, the segment will be resent. ( timeout retransmission )

3. When TCP receives data from the other end of the TCP connection, it will send an acknowledgment. This acknowledgment is not sent immediately and will usually be delayed by a fraction of a second. ( For the received request, give a confirmation response) (The reason for the delay may be to do a complete check of the packet ).
4. TCP will keep a checksum of its header and data . This is an end-to-end checksum to detect any changes in the data in transit. If there is an error in the checksum of the received segment, TCP will discard the segment and not acknowledge receipt of the segment. (If there is an error in the verification packet, the segment is discarded, and no response is given. TCP sends the data end, and the data will be resent when it times out.)
5. Since the TCP segment is transmitted as an IP datagram, and the arrival of the IP datagram May be out of sequence, and therefore TCP segments may arrive out of sequence. If necessary, TCP will reorder the received data, handing the received data to the application layer in the correct order. (Reorder the out-of-order data before handing it over to the application layer)
6. Since the IP datagram will be repeated, the TCP receiver must discard the repeated data. (For duplicate data, it can discard duplicate data )
7. TCP can also provide flow control . Each side of a TCP connection has a fixed amount of buffer space. The receiving end of TCP only allows the other end to send as much data as the receiving end's buffer can accommodate. This will prevent the faster host from overflowing the buffer of the slower host. (TCP can perform flow control to prevent faster hosts from overflowing buffers for slower hosts.) The flow control protocol used by TCP is a variable-size sliding window protocol.
8. TCP can also provide congestion control . When the network is congested, reduce the sending of data.

1.3

How to keep the connection after TCP establishes the connection (check if the connection is broken)? 

1. Keepalive mechanism implemented by TCP protocol layer

2. Self-implemented HeartBeat heartbeat package

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325206301&siteId=291194637