QUdpSocket Class

Inherited from QAbstractSocket class

The QUdpSocket class provides UDP sockets.

UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used in situations where reliability is not important. QUdpSocket is a subclass of QAbstractSocket that allows you to send and receive UDP datagrams.

The most common way to use this class is to use bind() to bind to an address and port, and then call writeDatagram() and readDatagram() / receiveDatagram() to transfer the data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to the peer by calling connectToHost().

A socket emits the bytesWritten() signal every time it writes a datagram to the network. If you just want to send datagrams, you don't need to call bind().

Whenever a datagram arrives, the readyRead() signal is emitted. In this case, hasPendingDatagrams() returns true. Call pendingDatagramSize() to get the size of the first pending datagram, and call readDatagram() or receiveDatagram() to read it.

NOTE: When you receive the readyRead() signal, the incoming datagram should be read, otherwise the signal will not be emitted for the next datagram.

QUdpSocket also supports UDP multicast. Use joinMulticastGroup() and leaveMulticastGroup() to control group membership, and QAbstractSocket::MulticastTtlOption and QAbstractSocket::MulticastLoopbackOption to set TTL and loopback socket options. Use setMulticastInterface() to control the outbound interface of multicast datagrams, and use multicastInterface() to query.

Using QUdpSocket, you can also use connectToHost() to establish a virtual connection to a UDP server, and then use read() and write() to exchange datagrams without specifying the recipient of each datagram.

The Broadcast Sender, Broadcast Receiver, Multicast Sender, and Multicast Receiver examples illustrate how to use QUdpSocket in applications.

 

Returns true if there is at least one datagram waiting to be read; otherwise returns false.

Returns the size of the first pending UDP datagram, or -1 if no datagram is available. 

 Receives datagrams no larger than maxSize bytes and stores them in data. The sender's host address and port are stored in *address and *port (unless the pointer is 0).

Returns the size of the datagram on success; otherwise returns -1.

If maxSize is too small, the rest of the datagram will be lost. To avoid data loss, call pendingDatagramSize() to determine the size of a pending datagram before attempting to read it. If maxSize is 0, the datagram is discarded.

Receives a datagram no larger than maxSize bytes and returns it, along with the sender's host address and port, in a QNetworkDatagram object. If possible, the function will also attempt to determine the datagram's destination address, port, and hop count upon receipt.

On failure, a QNetworkDatagram reporting an invalid report is returned.

If maxSize is too small, the rest of the datagram will be lost. If maxSize is 0, the datagram is discarded. If maxSize is -1 (the default), the function will attempt to read the entire datagram. 

 Send a datagram of size size to the host address of port port. Returns the number of bytes sent on success; otherwise returns -1.

Datagrams are always written as one block. The maximum size of a datagram is highly platform dependent, but can be as low as 8192 bytes. If the datagram is too large, this function will return -1 and error() will return DatagramTooLargeError.

It is generally not recommended to send datagrams larger than 512 bytes because even if they are sent successfully, they may be fragmented by the IP layer before reaching the final destination.

Warning: Calling this function on a connected UDP socket may result in an error and no packets being sent. If you are using a connected socket, use write() to send the datagram.

This is an overloaded function.

Send the datagram to the host address and port number contained in the datagram, using the network interface and hop limit also set there. If the destination address and port number are not set, this function will send to the address passed to connectToHost(). If the destination address is IPv6 and the scope id is non-null but different from the interface index in the datagram, it is not certain which interface the operating system will choose to send.

If successful, the function returns the number of bytes sent, or -1 if an error is encountered.

Warning: Calling this function on a connected UDP socket may result in an error and no packets being sent. If you are using a connected socket, use write() to send the datagram.

This is an overloaded function.
Send datagram datagram to host address host and port port.
If successful, the function returns the number of bytes sent, or -1 if an error is encountered. 

Guess you like

Origin blog.csdn.net/m0_46521579/article/details/132634892