This paper is participating in theNetwork protocols must be known and must be known”Essay campaign

preface

Both TCP and UDP work at the transport layer, and their purpose is to pass data through programs. To them, the data is binary numbers, which are nothing in themselves. The most significant difference between them is that TCP is connection-based while UDP is not. Next, I will take you to explain the differences between them in detail.

TCP

1. In this paper,

Transmission Control Protocol (TCP) is one of the main network protocols, which can ensure that data is delivered accurately according to the order in which packets are sent. TCP was first devised in the 1970s by Vint Cerf and Bob Kahn, two DARPA scientists who were then called the fathers of the Internet.

2. Packet structure

TCP packet structure is quite complex, frankly speaking, we send data to record the offset + data length + data content to complete. This way, if the sent data is stuck, or lost, the offset and length of the data can be used to locate and retransmit the data in the correct order.

3. Data transmission

A. Three handshakes:

  • Our client sends a packet of connection request data (SYN packet) to the server, asking if we can establish a connection with you.
  • If the peer agrees to connect, a SYN+ACK packet is sent to the client.
  • After receiving the packet, the client sends an ACK packet to the server to complete the connection.

Because this process has three packets of data sent to each other, it is called a three-way handshake. But why three packs? Can’t we just send each other one time? The reason is to prevent invalid request packets from causing errors. In other words, SYN+ACK packets need to be sent to verify whether the connection is current. In this way, the server can avoid the misunderstanding of multiple connections caused by data retention, which leads to unreliable network channels.

B. Wave four times:

We just completed the connection between client and server with a three-way handshake. And four waves, as the name implies, is through four communications, to tell the other end to shut down. Of course, both the client and the server can issue a close request.

If the client wants to disconnect the connection first, it sends a FIN request to the client, and the client enters the closed wait state. After receiving the request, the server sends an ACK to the client for confirmation, and the server also enters the closed wait state. It is not received at this point, because the server can still send unsent data, and the client can still receive data. The server sends a FIN packet for final confirmation, and then enters the waiting state again. When the client sends an ACK for confirmation, the connection is terminated.

UDP

1. In this paper,

User Data Packet Protocol (UDP) is a simple data packet – oriented communication protocol at the transport layer of the OSI model. UDP provides only unreliable delivery of data, and does not keep a backup of data once it has sent data from the application to the network layer (hence UDP is sometimes referred to as an unreliable data packet protocol).

2. Packet structure

UDP adds only reuse and validation fields to the header of the IP packet. Unlike TCP, UDP directly sends packets as pure data. Packets are sent without parameters such as offset and length. Therefore, the receiving order cannot be recorded and may be out of order.

3. Data transmission

Simply wrap the data and send it directly from the network card, that’s it. Packet before there is any connection on the state, so he is connectionless fast, but packet loss, out of order problems may also occur.

conclusion

Through the above introduction, I believe you have a simple understanding of TCP and UDP. To put it more figuratively, TCP is like when we are on the phone, you can establish a connection with the object exactly, every word you say, the other party can receive a response, if you hang up, he will also interrupt. UDP, on the other hand, is more like writing a letter, sending a message directly to the other person without considering the connection to the object, but it is not clear whether the message will be lost, delayed or in the wrong order, etc. There is no saying who is good and who is bad, and the pros and cons can be determined according to different business scenarios.

A simple comparison can be made:

TCP protocol UDP protocol.
Establish a connection Need to be There is no connection
Resource consumption more less
The connection speed slow fast
reliability Stable and reliable Packets are easily lost and may be out of order
Applicable scenario Send emails, transfer files, view web pages, etc Voice calls, live video, competitive games, etc

extension

We do not have much objection to TCP, and UDP may feel some chicken rib, then there is a protocol – RUDP, as a reliable UDP upgrade version, it will not be like UDP will completely lose data packets, it will be more than a step to confirm data packets to complete the loss of data retransmission, but also can do flow control and buffering.