2. boost asio tutorial---Tcp and Udp

There are two main transport layer protocols that we will use - TCP and UDP. A protocol is a set of protocols that specifies how data transfer should work.

Transmission Control Protocol - TCP In short, a TCP connection is almost like a file - you open it, you read some data from it, you write some other data, and when you're done you close it. There are some limitations though:

When working with a file, you can ask for its size. In the case of a TCP connection you cannot do this.

When processing a file, you can position the read or write pointer. You can't do this with a TCP connection either.

In other words, a file gives you random access, whereas a TCP connection is a bidirectional sequential flow.

User Datagram Protocol - UDP Transmitting data over UDP represents the exchange of solid pieces of data. Compared to TCP, there is no such thing as UDP connection. You cannot receive part of a data piece that has been sent to the application. You either get nothing or the entire portion. Here's what you need to know about UDP so far:

Since it is not a stream, there is no UDP connection. So you don't need to connect or close the UDP socket. You just need to send data or receive data.

The buffer used to receive UDP packets must be large enough to contain part of the entire packet, otherwise you will receive nothing. Therefore, you need to know the upper limit size of the packets you will receive.

The order of incoming packets is usually different from the order in which they were sent. This means you need to maintain the order yourself.

There is actually no guarantee that you will receive all packets sent to your application. This means UDP packet loss is common. This in turn means you need to control the delivery of UDP packets yourself.

As you can see, UDP is a bit trickier than TCP. But it still has advantages that we'll discuss later.

Here's everything you need to know about the agreement right now. we can move on

Guess you like

Origin blog.csdn.net/Knowledgebase/article/details/132716622