Android development knowledge learning-TCP/IP protocol family

Learning resources come from: Throwing Object Line

TCP/IP protocol suite

  • A network layered model composed of a series of protocols
  • Why layer?
    Because of the instability of the network
    Insert image description here
  • Application Layer Application layer: HTTP, FTP, DNS, provides communication mechanism between network applications
  • Transport Layer: TCP, UDP, a layer that provides reliable end-to-end data transmission services
  • Internet Layer Network layer: IP, the entrance and exit of all data transmission in the network
  • Link Layer Data link layer: Ethernet, Wi-Fi, handles the physical interface details with transmission media (such as twisted pair, optical fiber, radio waves, etc.)
    Insert image description here

TCP connection

What is a connection?
The two communicating parties confirm that "communication is possible" and will not discard the other party's messages, which is "establishing a connection."

Establishment and closure of TCP connections

Establishment of TCP connection

Insert image description here

① First, the client sends a SYN packet to the server and waits for confirmation from the server, where:

  • The flag bit is SYN, indicating a request to establish a connection;
  • The sequence number is Seq = x (x is generally a random number);
  • Then the client enters the SYN-SENT phase.

② After receiving the SYN packet from the client, the server confirms the packet, ends the LISTEN phase, and returns a TCP message, including:

  • The flag bits are SYN and ACK, indicating that the client's message Seq sequence number is confirmed to be valid, the server can normally receive the data sent by the client, and agrees to create a new connection;
  • The sequence number is Seq = y;
  • The confirmation number is Ack = x+ 1, which means that it receives the client's sequence number Seq and adds 1 to its value as the value of its own confirmation number Ack, and then the server enters the SYN-RECV stage.

③ After the client receives the sent SYN + ACK packet, it is clear that the data transmission from the client to the server is normal, thus ending the SYN-SENT phase. And return the last segment of the message. in:

  • The flag bit is ACK, indicating confirmation of receipt of a signal from the server agreeing to connect;
  • The sequence number is Seq = x + 1, which means that the confirmation number Ack from the server is received and its value is used as its own sequence number value;
  • The confirmation number is Ack= y + 1, which means that the server-side sequence number seq is received, and its value is added by 1 as the value of its own confirmation number Ack.
  • Then the client enters ESTABLISHED.
    When the server receives a message from the client confirming receipt of server data, it learns that the data transmission from the server to the client is normal, thus ending the SYN-RECV phase and entering the ESTABLISHED phase, thereby completing the three-way handshake.
Why three handshakes?

Check whether the current network situation meets the basic conditions for reliable transmission.
In order to prevent the invalid connection request segment from suddenly being transmitted to the server, causing an error.

Specific example: "Invalid connection request segment" occurs in such a situation: the first connection request segment sent by the client is not lost, but stays in a certain network node for a long time. , causing the delay to arrive at the server some time after the connection is released. It turns out that this is a message segment that has long since expired. However, after the server receives this invalid connection request segment, it mistakenly thinks that it is a new connection request sent by the client again. So it sends a confirmation message segment to the client and agrees to establish the connection. Assuming that the "three-way handshake" is not used, as long as the server sends a confirmation, a new connection is established. Since the client has not issued a request to establish a connection, it will not pay attention to the server's confirmation 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. The "three-way handshake" method can prevent the above phenomenon from happening. For example, in the situation just now, the client will not send a confirmation to the server's confirmation. Since the server cannot receive the confirmation, it knows that the client did not ask to establish a connection.

Closing of TCP connection

After the client and server establish a TCP connection through a three-way handshake, when the data transmission is completed, the TCP connection must be disconnected. For TCP disconnection, there is the mysterious "four waves".

Insert image description here

For example, the serial number initialized by the client is ISA=100, and the serial number initialized by the server is ISA=300. After the TCP connection is successful, the client sends a total of 1000 bytes of data, and the server replies with a total of 2000 bytes of data before the client sends a FIN message.

  • The first wave: When the client's data is transmitted, the client sends a connection release message to the server (of course, it can also send a connection release message and stop sending data when the data has not been sent). The connection release message contains FIN Flag bit (FIN=1), sequence number seq=1101 (100+1+1000, 1 is a sequence number occupied when establishing a connection). It should be noted that after the client sends the FIN segment, it cannot send data, but it can still receive data normally; in addition, even if the FIN segment does not carry data, it still occupies a sequence number.

  • The second wave: After receiving the FIN message sent by the client, the server replies with a confirmation message to the client. The confirmation message contains the ACK flag bit (ACK=1) and the confirmation number ack=1102 (client FIN message sequence No. 1101+1), serial number seq=2300(300+2000). At this time, the server is in a closed waiting state instead of immediately sending a FIN message to the client. This state will continue for a while because the server may still have data that has not been sent.

  • The third wave: After the server completes sending the last data (such as 50 bytes), it sends a connection release message to the client. The message contains the FIN and ACK flag bits (FIN=1, ACK=1), and the confirmation number. Just like the second wave, ack=1102 and sequence number seq=2350(2300+50).

  • The fourth wave: After receiving the FIN message from the server, the client sends a confirmation message to the server. The confirmation message contains the ACK flag (ACK=1), confirmation number ack=2351, and sequence number seq=1102. . Note that after the client sends the confirmation message, it does not release the TCP connection immediately. Instead, it waits for 2MSL (twice the longest segment life) before releasing the TCP connection. Once the server receives the confirmation message from the client, it will immediately release the TCP connection, so the server ends the TCP connection earlier than the client.

Why wave four times?

The TCP protocol is a connection-oriented, reliable, byte stream-based transport layer communication protocol. TCP is in full-duplex mode , which means that when host 1 sends a FIN segment, it only means that host 1 has no data to send. 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 message 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 both parties will happily interrupt the TCP connection.

Why do you need a long connection?

Because the mobile network is not in the Internet, but in the operator's intranet, it does not have a real public IP. Therefore,
when a TCP connection does not communicate for a period of time, the gateway will close this connection for network performance considerations.
The connection channel between the TCP connection and the public network causes the TCP port to no longer receive external communication messages, that is, the TCP
connection is passively closed.

ⓓThe implementation method of long connection
is heartbeat. That is, within a certain interval, the TCP connection is used to send ultra-short meaningless messages so that the gateway cannot
define itself as an "idle connection", thereby preventing the gateway from closing its own connection.

Common interview questions

  • Why does the TCP connection take 3 times? Isn’t it possible to do it twice?
    Because the problem of packet loss during connection needs to be considered, if there are only 2 handshakes, if the confirmation message segment sent by the server to the client is lost during the second handshake, the server is already ready to send and receive data (it can be understood that the server has The connection is successful), but the client has not received the confirmation message from the server, so the client does not know whether the server is ready (it can be understood that the client has not connected successfully). In this case, the client will not give When the server sends data, it will also ignore the data sent by the server. If it is a three-way handshake, there will be no problem even if packet loss occurs. For example, if the confirmation ack message sent by the client in the third handshake is lost, and the server does not receive the confirmation ack message within a period of time, it will retry the third handshake. Two-step handshake, that is, the server will resend the SYN segment. After receiving the resent segment, the client will send an acknowledgment ack message to the server again.

  • Why is it 3 times when TCP is connected but 4 times when it is closed?
    Because TCP can only be disconnected when neither the client nor the server has data to send. When the client sends a FIN message, it can only guarantee that the client has no data to send. The client does not know whether the server has any data to send. After receiving the client's FIN message, the server can only reply to the client with a confirmation message to tell the client that the server has received your FIN message, but there is still some data that has not been sent. The server can only send a FIN message to the client after all the data has been sent (so the confirmation message and FIN message cannot be sent to the client at once, because there is an extra time here).

  • Why does the client have to wait 2MSL before releasing the TCP connection after sending the fourth wave confirmation message?
    Here we also need to consider the issue of packet loss. If the message for the fourth wave is lost and the server does not receive the confirmation ack message, it will resend the message for the third wave. This will take the longest time for the message to go back. It is 2MSL, so it takes such a long time to confirm that the server has indeed received it.

After-school questions

1. [Single-choice question] In the TCP/IP protocol suite, which layer is the HTTP protocol at?
A. Application layer
B. Transport layer
C. Network interconnection layer
D. Data link layer

Answer: A
Answer analysis: HTTP (Hypertext Transfer Protocol) protocol is an application layer protocol, which is used to transmit hypertext (such as web pages) in the network. In the TCP/IP protocol suite, the HTTP protocol is located at the top layer, which is the application layer.

2. [Single-choice question] In the TCP/IP protocol suite, which layer is the TCP protocol located at?
A. Application layer
B. Transport layer
C. Network interconnection layer
D. Data link layer

Answer: BAnswer
analysis: TCP (Transmission Control Protocol) protocol is a transport layer protocol, which is located at the transport layer in the TCP/IP protocol suite. The TCP protocol is responsible for segmenting data and providing reliable transmission, ensuring that the data can be correctly transmitted to the destination address in the network

3. [Single-choice question] In the TCP/IP protocol suite, which layer is the IP protocol located at?
A. Application layer
B. Transport layer
C. Network interconnection layer
D. Data link layer

Answer: C
Answer analysis: IP protocol (Internet Protocol) is one of the core protocols in the TCP/IP protocol suite. It is located at the network interconnection layer. The IP protocol is responsible for sending data packets from one network to another, ensuring the routing and transmission of data.

4. [Single-choice question] What is the main function of the TCP protocol?
A. Conduct specific network application interactions
B. Ensure reliable transmission of network data
C. Solve problems of transmitting packets on the network, such as network addressing and sending network data in packet units

Answer: B
Answer analysis: The main function of TCP (Transmission Control Protocol) is to ensure reliable transmission of network data. The TCP protocol is a connection-oriented protocol that provides a reliable, ordered and error-checking data transmission method. In the TCP protocol, a connection is established between the sender and the receiver, and then reliable transmission of data is ensured through mechanisms such as confirmation, sequence number, and flow control. In contrast, application layer protocols (such as HTTP) are mainly responsible for specific network application interactions, while network layer protocols (such as IP) are mainly responsible for solving problems of transmitting packets on the network, such as network addressing and sending network packets in units of packets. data. Therefore, the main role of the TCP protocol is to ensure reliable transmission of network data

5. [Single-choice question] What is the main function of the IP protocol?
A. Conduct specific network application interactions
B. Ensure reliable transmission of network data
C. Solve problems of transmitting packets on the network, such as network addressing and sending network data in packet units

Answer: C
Answer analysis: The IP protocol is one of the core protocols in the TCP/IP protocol suite. It is responsible for sending data packets from one network to another and ensuring the routing and transmission of data. The IP protocol implements data routing and transmission by dividing data into data packets and including source IP address and destination IP address in each data packet. Therefore, the main function of the IP protocol is to solve the problem of transmitting packets on the network, such as network addressing and sending network data in packet units.

  1. [Essay title] Briefly describe the "three-way (three-way) handshake" when establishing a TCP connection

Answer: The "three-way handshake" when establishing a TCP connection means that in the TCP protocol, three data packets need to be sent to confirm the establishment of the connection. The specific process is as follows:

  1. SYN: First, the client sends a packet with the SYN (synchronization) flag to the server. This packet will contain a randomly generated sequence number, assumed to be X.
  2. SYN-ACK: After receiving the SYN packet, the server will send a data packet with SYN and ACK (acknowledgement) flags to the client. This data packet contains two sequence numbers, one is randomly generated by the server itself, assumed to be
    Y, and the other is the confirmation number to the client. This confirmation number is equal to the client's sequence number plus one, that is, Y = X + 1.
  3. ACK: Finally, the client sends an ACK packet to the server, whose sequence number is equal to the server's sequence number plus one, that is, Y = X + 2. In this way, the three-way handshake process of the TCP connection is completed, thus establishing a reliable connection.
  1. Why is TCP a three-way handshake instead of a two-way handshake?

Answer: The TCP protocol uses a three-way handshake to ensure that the connection is successfully established, to confirm receipt of the data packet and the other party's initial sequence number, and to prevent the expired connection request message from being suddenly transmitted to server 1.

If two handshakes are used, in the first handshake, the client sends a SYN packet, and the server does not send a confirmation message after receiving it. The client resends the SYN packet after timeout. If there is a retransmission mechanism in the network, The client may receive old SYN packets, causing connection establishment error 1.

  1. [Essay title] Briefly describe the "four waves" when the TCP connection is closed

Answer: The "four waves" when TCP connection is closed means that in the TCP protocol, four data packets need to be sent to confirm the closure of the connection when closing the connection. The specific process is as follows:

  1. FIN: First, the client sends a data packet with a FIN (end) flag to the server to close the data transfer from the client to the server.
  2. ACK: After the server receives the FIN, it sends a data packet with an ACK (confirmation) flag to the client. The confirmation sequence number is the received sequence number plus one, and the server enters the CLOSE_WAIT state.
  3. FIN: Then, the server sends a data packet with the FIN flag to the client to close the data transfer from the server to the client, and the server enters the LAST_ACK state.
  4. ACK: Finally, after receiving the FIN, the client sends a data packet with an ACK flag to the server. The confirmation sequence number is the received sequence number plus one. The server enters the CLOSED state and completes four waves.
  1. [Essay question] Why does the TCP connection wave four times instead of three times when it is disconnected?

Answer: The TCP connection uses four waves instead of a three-way handshake when disconnected, mainly to consider reliability issues
. During the three-way handshake, if the last ACK packet is lost, the client will resend a FIN packet. , and the server will enter the TIME_WAIT state because it cannot receive the confirmation. In this case, the server waits for a while to ensure that all packets have been sent before closing the connection. However, the server cannot accept new connections during this period, resulting in a waste of resources.
To solve this problem, the TCP protocol uses a four-waving process. After the fourth wave, the server can release the connection immediately without waiting for a while. This way, the server can utilize resources more efficiently while also reducing the number of packets between the client and the server.
Therefore, the purpose of using four waves instead of three handshakes when TCP connections are disconnected is to improve the reliability of the connection and optimize resource utilization.

Guess you like

Origin blog.csdn.net/weixin_74239923/article/details/134112787