TCP/IP model

Transmission Control Protocol (TCP) /Internet Protocol (IP), which is the core Protocol of the Internet, contains a series of network protocols that form the basis of the Internet.

The TCP/ IP-based reference model divides protocols into four layers, which are link layer, network layer, transport layer and application layer. The following figure shows the comparison between the TCP/IP model and the OSI model.

2. Protocols related to each network layer

  • Are many application layer protocol, HTTP, FTP, belong to the application layer protocol for application layer protocol in the format as a general request {protocal} : / / IP | domain
  • TCP/UDP At the transport layer, TCP unpacks data, establishes links, and transmits network data to a specified IP address in the form of data packets.
  • The IP protocol, applied at the network layer, is responsible for adding IP addresses to data and routing target server locations through network IP addresses.
  • ARP is used at the data link layer to obtain MAC addresses based on IP addresses. RARP obtains IP addresses based on MAC addresses.

Three, TCP and UDP differences

TCP and UDP are both transport protocols, but they have different features and application scenarios. The following uses ICONS for comparison and analysis. For a messageIn packet – oriented transmission mode, the application layer sends UDP packets as long as they are received. That is, UDP sends one packet at a time. Therefore, the application must select a message of the appropriate size. If the packet length is too long, the IP layer needs to fragment the packet, which reduces the efficiency. If it is too short, the IP address is too small.Word oriented streamFor byte streams, applications interact with TCP one byte at a time, but TCS refer to applications as a series of unstructured byte streams. The length of packets transmitted by TCP is fixed.

TCP is used when the quality of network communication is not required, such as HTTP, HTTPS, FTP, POP, and SMTP.

UDP scenarios: When the requirements for network communication quality are not high and the network communication speed is required to be as fast as possible, UDP can be used. Instant messaging software basically uses UDP protocol for communication.

Four, DNS

The Domain Name System (DNS) is a distributed database that maps Domain names and IP addresses on the Internet. It enables users to access the Internet more conveniently without having to remember IP numbers that can be read directly by machines. The process of obtaining an IP address from a host name is called domain name resolution (or hostname resolution). DNS runs on TOP of UDP and uses port 53.

Five, three handshakes

TCP is connection-oriented. Before either party can send data to the other, a connection must be established between the two parties. In the TCP/IP protocol, TCP provides reliable connection service, which is initialized by three-way handshake. The purpose of the three-way handshake is to synchronize the serial number and confirmation number of the two connected parties and exchange TCP window size information. First handshake: Establishes a connection. The client sends a connection request packet segment and sets the SYN position to 1 and Sequence Number to X. The client then enters the SYN_SEND state and waits for confirmation from the server.

Second handshake: Indicates that the server receives a SYN packet. 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 (that is, the SYN+ACK packet segment) and sends it to the client. Then the server enters the SYN_RECV state.Third handshake: The client received a SYN+ACK packet from the server. A. Then set the Acknowledgment Number to Y +1 and want the server to send an ACK segment. After this segment is sent, both the client and the server enter the ESTABLISHED state and complete the TCP three-way handshake.

Six or four waves

When the client and the server establish a TCP connection through the three-way handshake, when the data transmission is completed, it must test the TCP connection, so for the TCP connection, there is the concept of “four-way wave” First wave: Set the Sequence Number for host 1 (client or server) to send a FIN packet segment to host 2. At this point, host 1 enters the FIN_WAIT_1 state. This means that host 1 has no data to send to host 2.

Second wave: Host 2 receives the FIN segment sent by host 1 and sends an ACK segment back to host 1. This Acknowledgment Number is set to Sequence Number plus 1. Host 1 enters the FIN_WAIT_2 state. Host 2 tells host 1 that I “agree” to your shutdown request;

Third waveHost 2 sends a FIN packet to host 1 to close the connection and enters the LAST_ACK state.

Fourth waveHost 1 receives the FIN packet from host 2 and sends an ACK packet to host 2. Then host 1 enters the TIME_WAIT state. Host 2 closes the connection after receiving the ACK packet from host 1. If host 1 does not receive a reply after waiting for 2MSL, then the Server is shut down.

Why do you need to wave four times? The TCP request is a full-duplex request. Both hosts can send data, so they notify each other that no data needs to be sent.

Why wait 2MSL: MSL: indicates the maximum lifetime of a packet segment. It is the maximum time for any packet segment to stay in the network before being discarded. There are two reasons:

  • Ensure that TCP full-duplex connections can be closed reliably
  • Ensure that duplicate data segments are removed from the network

First point: If host 1 is directly CLOSED, host 2 does not receive the last recovered ACK from host 1 due to IP protocol unavailability or other network reasons. Then host 2 will continue to send the FIN after the supermarket. At this time, since host 1 has been CLOSED, the connection corresponding to the resend FIN cannot be found. Therefore, host 1 cannot directly enter CLOSED. Instead, it should keep TIME_WAIT. When receiving a FIN again, host 1 can receive an ACK from the host and finally close 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 connection CLOSED by bar. That is, it is possible that the new connection and the old link have the same port number. In general, no problems are sent, but there are some special cases: Assuming a new connection and closed the old link port number is the same, if the previous connection and some data are still stranded in the network, the delay data after the connection is established to reach the host 2, because of the new connection and old connection port number is the same, the TCP protocol is considered the delay data belongs to a new connection, This will confuse the packet with the actual new connection.