“This is the 24th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Network model is not from the beginning, in the early development of the network, network protocols are defined by the Internet companies themselves. Because the network protocols of different companies are different, the protocols of different companies cannot communicate with each other because there is no unified standard network protocol. That was bad for the Web, and to deal with it, the OSI (Open Systems Interconnection Model), the International Organization for Standardization (ISO) standard in 1984, was a standard, not an implementation. TCP/IP protocol is designed based on this model.

TCP/IP model is a four-layer model, which consists of network interface layer, network layer, transport layer and application layer respectively from bottom to top

  • Network interface layer: The network driver that implements the network interface to handle the transfer of data over physical media such as Ethernet, token ring, and so on
  • Network layer: Selects routes and forwards packets
  • Transport layer: Provides end-to-end communication for host applications. The transport layer only cares about the origin and destination of the communication and does not care about the packet transfer process
  • Application layer: Handles the logic of the application

TCP

TCP header format

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 numbers and confirmation numbers of the two connected parties and exchange TCP window size information.

First handshake: Establish a connection. The client sends a connection request packet segment and sets the SYN position to 1 and Sequence Number to X. Then, the client enters the SYN_SEND state and waits for confirmation from the server.

Second handshake: The server receives a SYN packet segment. Context The server should acknowledge the SYN segment received from the client, and set this Acknowledgment Number to X +1(Sequence Number+1). Set the SYN position to 1 and Sequence Number to y. The server puts all the above information into a packet segment (namely, SYN+ACK packet segment) and sends it to the client. At this time, the server enters the SYN_RECV state.

Third handshake: The client receives a SYN+ACK packet from the server. A. Then set this Acknowledgment Number to Y +1 and send an ACK segment to the server. After this segment is sent, both the client and the server enter the ESTABLISHED state and complete the TCP three-way handshake.

Why three handshakes?

An error occurs in case an invalid connection request segment is suddenly sent to the server.

An example is as follows: Invalid connection request packet segment Is generated in this case: The first connection request packet segment sent by the client is not lost, but is detained on a network node for a long time. As a result, the packet reaches the server at a certain time after the connection is released. Originally, this is an invalid packet segment. However, after the server receives the invalid connection request packet segment, it mistakenly thinks it is a new connection request sent by the client. Then the client sends a confirmation message to agree to establish a connection. Assuming that the “three-way handshake” is not used, a new connection is established as soon as the server sends an acknowledgement. Since the client does not send a connection request, it ignores the server’s confirmation and does not send data to the server. However, the server assumes that the new transport connection has been established and waits for data from the client. As a result, many of the server’s resources are wasted. The three-way handshake prevents this from happening. For example, the client does not issue an acknowledgement to the server’s acknowledgement. Since the server receives no acknowledgement, it knows that the client has not requested a connection

Four times to wave

Why break up four times?

TCP is a connection-oriented, reliable, byte stream – based transport-layer communication protocol. TCP is in full-duplex mode. When host 1 sends a FIN packet, it only indicates that host 1 has no data to send. Host 1 tells host 2 that all data has been sent. However, at this point, host 1 can still accept data from host 2; When host 2 returns an ACK packet, it indicates that it knows that host 1 has no data to send, but host 2 can send data to host 1. When host 2 also sends a FIN packet segment, host 2 also has no data to send and informs host 1 that host 1 has no data to send, and both parties happily break the TCP connection.

Why wait until 2MSL?

MSL: The maximum lifetime of a packet segment, which is the maximum time that any packet segment is in the network before being discarded.

There are two reasons:

  1. Ensure that TCP full-duplex connections can be closed reliably
  2. Ensure that duplicate data on this connection is removed from the network

First point: If host 1 is directly CLOSED, host 2 does not receive the final ACK from host 1 due to the unreliability of IP protocol or other network reasons. Then host 2 will continue to send the FIN after the timeout. Since host 1 is already CLOSED, the connection corresponding to the resending FIN cannot be found. Therefore, host 1 does not directly enter CLOSED, but holds TIME_WAIT to ensure that the host receives an ACK when it receives a FIN again and closes the connection correctly.

Second point: if host 1 closes directly and then initiates a new connection to host 2, we cannot guarantee that the new connection will have a different port number from the one just CLOSED. This means that the new connection may have the same port number as the old connection. Generally not what problem, but there are special circumstances: suppose a new connection and closed the old connection port number is the same, if the previous connection remain some of the data in the network, the delay of data belongs to a new connection, so confused with a real new connection packet occurrence. Therefore, TCP connections must wait in TIME_WAIT state for 2x MSL to ensure that all data for this connection disappears from the network.