IP: sends data packets to the destination host
- The upper layer sends packets containing “geek time” to the network layer;
- The network layer attaches the IP header to the packet to form a new IP packet, which is handed to the bottom layer.
- The bottom layer transmits data packets to host B through the physical network.
- The packet is transmitted to the network layer of host B, where host B unwraps the IP header of the packet and passes the unwrapped data to the upper layer
- Eventually, the packet containing the geek time message reaches the upper layer of host B.
UDP: Sends packets to the application
Although UDP does not guarantee data reliability, it is very fast, so UDP will be used in some areas where speed is a concern but data integrity is not so strict, such as online video, interactive games and so on.
TCP: Sends data intact to the application
- Data packets are easily lost during transmission.
- Large files are broken into smaller packets for transmission. These packets take different routes and arrive at the receiver at different times. UDP does not know how to assemble these packets into a complete file.
Based on these two issues, we introduced TCP. Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream – based transport layer communication Protocol. Compared with UDP, TCP has the following characteristics:
- TCP provides a retransmission mechanism for packet loss.
- TCP introduces the packet sorting mechanism to ensure that out-of-order packets are combined into a complete file.
First, establish the connection phase. This phase establishes the connection between the client and server through a “three-way handshake.” TCP provides connection-oriented communication transport. Connection-oriented refers to the preparation work between the two ends before data communication begins. The three-way handshake means that when a TCP connection is established, the client and server send a total of three packets to confirm the connection.
Secondly, the data transmission stage. At this stage, the receiving end needs to confirm each packet. That is, after receiving the packet, the receiving end needs to send the confirmation packet to the sender. Therefore, if the sender does not receive the confirmation message from the receiver within a specified period after sending a data packet, the packet is considered lost and the retransmission mechanism is triggered. Similarly, a large file is divided into many small packets during transmission. After these packets arrive at the receiving end, the receiving end sorts them according to the sequence number in the TCP header to ensure complete data.
Finally, the disconnect phase. Once the data is transferred, the connection is terminated, which involves the final stage of “four waves” to ensure that both parties are disconnected.
Three-way handshake
TCP has six flag bits:
- SYN(synchronous), establishing connection.
- ACK (acknowledgement), confirmed.
- PSH (push), and transmission.
- FIN (finish), over.
- RST (reset), reset.
- URG (urgent), emergency.
On the first handshake, the client sends a SYN packet to the server (SYN=j)
On the second handshake, the server receives a SYN packet from the client, acknowledges the SYN packet (ACK= J +1) from the client, and sends a SYN packet (SYN= K) of its own. The second handshake combines the connection request from the confirmation client and the connection request from the sending server into one.
For the third handshake, the client receives a SYN+ACK packet from the server and sends an ACK packet (ACK = K +1) to the server. After the packet is sent, the client and server enter the ESTABLISHED state (TCP connection is successful) and complete the three-way handshake.
Four times to wave
On the first wave, the client sends a FIN packet (FIN= H) to the server, which shuts down data transmission from the client to the server.
After the second wave, the server receives the FIN packet from the client, confirms the FIN packet (ACK= H +1) from the client, and sends the packet to the client.
For the third wave, the server closes the connection to the client and sends a FIN packet (FIN= I) to the client.
After the fourth wave, the client receives the FIN packet sent by the server, confirms the FIN packet (ACK= I +1), and sends the PACKET to the server.