This is the third day of my participation in the August More text Challenge. For details, see: August More Text Challenge
The preface
Today we will learn how TCP/IP works in the Web world. This knowledge will give us an insight into how data is transferred in the Web world, which will help us learn about HTTP and optimize the performance of Web pages later.
IP (Network Protocol)
The transmission of Internet data is like the transmission of express delivery in life. There is a transmission and there is a reception. You only need to know the specific address of a user, so you can send something to him.
The computer’s address is called an IP address, and to visit any web site is really just your computer requesting information from another computer. If A packet is to be sent from host A to host B, the IP address of host B and host A itself is attached to the packet before transmission, so that the data transmission knows where to go and from whom.
This additional information is incorporated into a data structure called the IP header, which contains the IP version, source IP address, destination IP address, and time to live.
Let’s simply sort out the flow of a packet transfer.
- Host A sends the packets to the network layer for transmission.
- The network layer then appends IP headers to packets, combines them into new IP packets, and transmits them to the physical layer.
- The physical layer transmits data to host B.
- The network layer of host B receives the data packet, disconnects the IP header of the data packet, and sends the data part to the upper layer of host B.
- Host B receives the packet.
UDP (User Packet Protocol)
Why you need UDP
IP is a very low-level protocol, only responsible for sending the data packet to the other party’s computer, but the other party’s computer does not know which application to give the data packet to, QQ or wechat? Hence the need for a more detailed user packet protocol, known as UDP.
What is the port number
Port is the most important information in UDP, which is actually a number in essence. Every program that wants to access the network needs to bind a port number, through which UDP can send the specified data packets to the specified program.
IP uses THE IP address information to send packets to the specified computer, while UDP uses the port number to send packets to the correct program. As with the IP header, the port number is inserted into the UDP header, which is then combined with the original packet to form a new UDP packet. The UDP header contains information such as the destination port and the source port number.
Let’s simply organize the process of a packet transfer with UDP.
- The upper layer delivers the packet to the transport layer;
- The transport layer appends A UDP header to the packet, forming a new UDP packet, and then delivers the new UDP packet to the network layer.
- The network layer then appends the IP header to the packet to form a new IP packet and delivers it to the bottom layer.
- The packet is transmitted to the network layer of host B, where host B disassembles the IP header and delivers the disassembled data portion to the transport layer.
- At the transport layer, the UDP header in the packet is disassembled and the data portion is handed over to the upper-layer application based on the port number provided in the UDP.
- Finally, the packets are transferred to the host B upper layer application.
UDP pros and cons: UDP does not guarantee data reliability, but it is very fast, so it can be used in areas where speed is important but data integrity is not so strict, such as online video, interactive games, etc.
Transmission Control Protocol
There are two problems with using UDP to transport browser requests.
- Data packets are easily lost during transmission.
- Large files are split into smaller packets, which travel different routes and arrive at different times. UDP does not know how to assemble these packets and restore them to a complete file.
So out of these problems came TCP, a connection-oriented, reliable, bitstream-based transport-layer communication protocol. Compared with UDP,TCP has the following two features.
- TCP provides a retransmission mechanism in case of packet loss.
- TCP introduces a packet sorting mechanism to ensure that out-of-order packets are combined into a complete file.
TCP three-way handshake
Note: in THE TCP protocol, the active end of the request is called “client”, and the passive end of the connection is called “server”. Both the client and the server can send and receive data after the TCP connection is established. Initially, both the server and client are in the CLOSED state. Before communication can begin, each party must create its own transmission Control block (TCB). After the TCB is created, the server enters the LISTEN state and is ready to receive connection requests from clients.
The first handshake
The client sends a connection request packet segment to the server. In the header of the packet, SYN=1, ACK=0, and seq=x. After the request is SENT, the client enters the SYN-sent state.
Note 1: SYN=1, ACK=0 indicates that the packet is a connection request packet.
Note 2: x is the initial sequence number of the byte stream for this TCP communication. TCP specifies that a segment with SYN=1 cannot have a data segment, but consumes a sequence number.
Second handshake
When a server receives a connection request segment and agrees to connect, it sends a response: SYN=1, ACK=1, SEQ =y, ACK= x+1. After the reply is sent, the syn-RCVD state is entered.
Note 1: SYN=1, ACK=1 indicates that the packet is a response packet with connection agreement.
Note 2: seq=y indicates the initial sequence number of the byte stream to be sent when the server is the sender.
Note 3: ACK =x+1 indicates that the server expects the next datagram to send bytes with serial numbers starting from x+1.
The third handshake
After receiving the connection consent reply, the client also sends an acknowledgement packet to the server, indicating that the connection consent reply sent by the server has been successfully received. The header of the packet is ACK=1, seq=x+1, ACK= y+1. After sending this segment, the client enters the ESTABLISHED state. After receiving this response, the server enters the ESTABLISHED state. The connection is ESTABLISHED.
Why do you need three handshakes to establish a connection, but not two?
Prevents invalid connection request packet segments from being received by the server, resulting in errors.
Note: Invalid connection request: If a client loses a connection request to the server, the client waits for a timeout and then sends a connection request again. In this case, the previous connection request is “invalid”. If you only need two handshakes to establish a connection, the client is not much changed and still needs to get a reply from the server before entering the ESTABLISHED state, whereas the server enters the ESTABLISHED state after receiving the connection request. In this case, if the network is congested and the connection request sent by the client cannot reach the server for a long time, the client resends the request after timeout. If the server receives and acknowledges the reply correctly, the two parties start the communication and release the connection after the communication ends. At this point, if the failed connection request reaches the server, since there are only two handshakes, the server will enter the ESTABLISHED state upon receipt of the request, waiting to send data or actively sending data. However, the client has already entered the CLOSED state, and the server will wait forever, which wastes the connection resources of the server.
TCP’s four waves
The release of a TCP connection takes a total of four steps, hence the term “four waves.” As we know, TCP connections are bidirectional, so of the four waves, the first two waves are used to disconnect connections in one direction, and the second two waves are used to disconnect connections in the other direction.
First wave
If user A considers the data transmission complete, it needs to send A connection release request to user B. The request contains only the packet header, and the main parameters in the header are FIN=1 and SEq = U. Then, A enters the FIN-wait-1 state.
Note 1: FIN=1 indicates that the packet is a connection release request.
Note 2: seq= U, u-1 is the number of the last byte sent from A to B.
Second wave
When B receives the connection release request, it notifies the corresponding application that the connection from A to B has been released. In this case, B enters the close-wait state and sends A response to release the connection. The packet header contains ACK=1, SEq = V, ACK= U +1.
Note 1: ACK=1: Except for the TCP connection request packet segment, the ACK of all datagrams in the TCP communication is 1, indicating the response.
Note 2: seq= V, v-1 is the number of the last byte sent by B to A.
Note 3: ACK =u+1 indicates that you want to receive a segment starting from the u+1 byte, and that you have successfully received the first U bytes.
After receiving the response, user A enters the FIN-wait-2 state and waits for user B to send A connection release request. After the second wave, the connection between A and B is released. B will not receive any more data, and A will not send any more data. However, the connection between B and A still exists, and B can continue to send data to A.
Third wave
After sending all data to A, B sends A connection release request to A. The request header is FIN=1, ACK=1, SEq =w, ACK= U +1. B enters the last-ACK state.
The fourth wave
After receiving the release request, user A sends A confirmation reply to user B. In this case, user A enters the time-wait state. This state will last for 2MSL. If there is no request from B for resend during this period, the TCB will be entered into the CLOSED state and the TCB will be revoked. After RECEIVING the acknowledgement, B will enter the CLOSED state and cancel TCB.
Why does USER A enter the time-wait state and then enter the CLOSED state after 2MSL?
In order to ensure that B can receive A’s confirmation reply. If A enters the CLOSED state directly after sending the acknowledgement reply, then if the reply is lost, B will resend the connection release request after the timeout. However, A has been CLOSED and will not respond. Therefore, B can never be CLOSED normally.