QUdpSocket is a subclass of QAbstractSocket that inherits from QIODevice. So you can use QUdpSocket to send and receive data. The biggest difference between it and QTcpSocket is that there is no need to establish a connection before sending data.
1. UDP is a connectionless, unreliable datagram transmission protocol.
2, the socket can be used as an input and output device, QUdpSocket can call writeDatagram () and readDatagram () to read and write the socket. The bytesWritten() signal is released each time a datagram is written.
3, QUdpSocket must first call the bind() function to bind before reading. If it is just written, it does not need to be bound.
4. When a datagram is readable, QUdpSocket will issue a readRead() signal, which can be read by defining a slot function associated with the signal. At this point hasPendingDatagrams() will also return true, pendingDatagramSize() can be used to get the length of the datagram, and then call the read function to read the data.
Detailed UDP protocolThe UDP header consists of four fields, each of which occupies 2 bytes, as follows: Port number, destination port number, datagram length, check value
The UDP protocol uses its port number to reserve its own data transmission channel for different applications. The UDP and TCP protocols use this mechanism to support the simultaneous transmission and reception of data by multiple applications at the same time. The data sender (which can be the client or server) sends the UDP datagram through the source port, while the data receiver receives the data through the destination port. Some network applications can only use static ports reserved or registered for them; others can use unregistered dynamic ports. Because the UDP header uses two bytes for the port number, the valid range for the port number is from 0 to 65535. In general, port numbers greater than 49151 represent dynamic ports.
The length of the datagram refers to the total number of bytes including the header and data portion. Because the length of the header is fixed, this field is primarily used to calculate the variable length portion of the data (also known as the data payload). The maximum length of a datagram varies depending on the operating environment. In theory, the maximum length of a datagram containing a header is 65535 bytes. However, some practical applications tend to limit the size of the datagram, sometimes to 8192 bytes.
The UDP protocol uses the checksum in the header to ensure data security. The check value is first calculated by the data sender through a special algorithm, and needs to be recalculated after being passed to the receiver. If a datagram is falsified by a third party during transmission or is damaged due to line noise, etc., the checksum calculation values ​​of the sender and the receiver will not match, and the UDP protocol can detect whether an error has occurred. This is different from the TCP protocol, which requires a check value.
UDPvs.TCPThe main difference between the UDP and TCP protocols is that the two differ in how they implement reliable delivery of information. The TCP protocol includes a special delivery guarantee mechanism. When the data receiver receives the information from the sender, it will automatically send an acknowledgment message to the sender; the sender will continue to transmit other information only after receiving the acknowledgment message. Otherwise it will wait until a confirmation message is received.
Unlike TCP, the UDP protocol does not provide a guarantee mechanism for data transfer. If there is a loss of datagrams during the delivery from the sender to the receiver, the protocol itself cannot make any detections or prompts. Therefore, the UDP protocol is often referred to as an unreliable transport protocol.
Another difference with the UDP protocol over the TCP protocol is how to receive multiple datagrams that are rigorous. Unlike TCP, UDP does not guarantee the order in which data is sent and received. For example, a client-side application sends the following 4 datagrams to the server.
D1
D22
D333
D4444
However, it is possible for UDP to submit the received data to the application of the server in the following order:
D333
D1
D4444
D22
In fact, this disorder of the UDP protocol is rarely seen, and it usually only happens when the network is very crowded.
Several features of the UDP protocol1) UDP is a connectionless protocol. Before the data is transmitted, the source and the terminal do not establish a connection. When it wants to transmit, it simply grabs the data from the application and throws it on the network as quickly as possible. On the sending end, the speed at which UDP transfers data is only limited by the speed at which the application generates data, the capabilities of the computer, and the bandwidth of the transmission; at the receiving end, UDP puts each message segment in a queue, and the application is queued each time. Read a message segment.
(2) Since the transmission data does not establish a connection, there is no need to maintain the connection status, including the transmission and reception status, so that one server can transmit the same message to multiple clients at the same time.
(3) The header of the UDP packet is very short, only 8 bytes, and the overhead of the 20-byte packet relative to TCP is small.
(4) The throughput is not regulated by the congestion control algorithm, and is limited only by the rate at which the application software generates data, the transmission bandwidth, and the performance of the source and end hosts.
(5) UDP uses the best effort to deliver, that is, does not guarantee reliable delivery, so the host does not need to maintain a complex link state table (there are many parameters).
(6) UDP is message oriented. The sender's UDP packet to the application is delivered to the IP layer after adding the header. Instead of splitting or merging, the boundaries of these messages are preserved, so the application needs to choose the appropriate message size.
Although UDP is an unreliable protocol, it is an ideal protocol for distributing information. For example, report stock market on the screen, display aviation information on the screen, and so on. UDP is also used in the routing information protocol RIP (RouTIng InformaTIon Protocol) to modify the routing table. In these applications, if a message is lost, another new message will be replaced after a few seconds. UDP is widely used in multimedia applications. For example, RealAudio software developed by Progressive Networks, which is a software that transmits pre-recorded or live music to clients on the Internet in real time. The software uses RealAudio audio-on-demand. The protocol protocol is a protocol that runs on top of UDP. Most Internet telephony software products also run on top of UDP.
2.00Mm Pin Header Connector,2.0Mm Dip Vertical Type Connector,2.00Mm Pitch Vertical Type Connector,Dip Vertical Type Pin Header Connector
Shenzhen CGE Electronic Co.,Ltd , https://www.cgeconnector.com