[Unity3D self-study record] The difference between TCP&UDP in network programming

TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-based protocol, that is, before sending and receiving data, a reliable connection must be established with the other party. A TCP connection must go through three "dialogues" before it can be established. The process is very complicated. We will only give a simple and vivid introduction here, as long as you can understand the process. Let's take a look at the simple process of these three conversations: host A sends a connection request packet to host B: "I want to send you data, can you?", this is the first conversation; host B sends a data packet to host A agreeing to connect and requesting synchronization (synchronization means that one of the two hosts is sending, the other is receiving, and coordinate work): "Yes, when will you send it?" This is the second conversation; The purpose of the three "dialogues" is to synchronize the sending and receiving of data packets. After three "conversations", host A officially sends data to host B. 


UDP (User Data Protocol, User Datagram Protocol) is a protocol corresponding to TCP. It is a non-connection-oriented protocol, it does not establish a connection with the other party, but sends the data packet directly! UDP is suitable for application environments that only transmit a small amount of data at a time and do not require high reliability. For example, we often use the "ping" command to test whether the TCP/IP communication between two hosts is normal. In fact, the principle of the "ping" command is to send a UDP data packet to the other host, and then the other host confirms the receipt of the data packet. If the message of whether the data packet arrives is returned in time, then the network is connected. For example, in the default state, a "ping" operation sends 4 packets (as shown in Figure 2). As you can see, the number of data packets sent is 4, and the number of packets received is also 4 (because the other host will send back a data packet to confirm receipt). This fully shows that the UDP protocol is a non-connection-oriented protocol, and there is no process of establishing a connection. Because the UDP protocol has no connection process, its communication effect is high; but because of this, its reliability is not as high as the TCP protocol. QQ uses UDP to send messages, so sometimes messages cannot be received.


The explanation of TCP and UDP in these two paragraphs is still very straightforward.

In fact, the difference between TCP and UDP is still obvious

TCP is connection-oriented, while UDP is connectionless

TCP transmission is very reliable, but unreliable compared to UDP

TCP should be used to transmit large amounts of data, while UDP is suitable for small amounts of data

Finally, the most important thing is that the speed of TCP is slow, but it is faster than UDP.

If you have any questions, you can tell me at any time so that I can correct them in time. Thank you for your support.

Guess you like

Origin blog.csdn.net/hackdjh/article/details/40977675