![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\reentrant More...
#include <qudpsocket.h>
Public Member Functions | |
QUdpSocket (QObject *parent=nullptr) | |
Creates a QUdpSocket object. | |
virtual | ~QUdpSocket () |
Destroys the socket, closing the connection if necessary. | |
bool | joinMulticastGroup (const QHostAddress &groupAddress) |
bool | joinMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface) |
bool | leaveMulticastGroup (const QHostAddress &groupAddress) |
bool | leaveMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface) |
QNetworkInterface | multicastInterface () const |
void | setMulticastInterface (const QNetworkInterface &iface) |
bool | hasPendingDatagrams () const |
Returns true if at least one datagram is waiting to be read; otherwise returns false . | |
qint64 | pendingDatagramSize () const |
Returns the size of the first pending UDP datagram. | |
QNetworkDatagram | receiveDatagram (qint64 maxSize=-1) |
qint64 | readDatagram (char *data, qint64 maxlen, QHostAddress *host=nullptr, quint16 *port=nullptr) |
Receives a datagram no larger than maxSize bytes and stores it in data. | |
qint64 | writeDatagram (const QNetworkDatagram &datagram) |
qint64 | writeDatagram (const char *data, qint64 len, const QHostAddress &host, quint16 port) |
Sends the datagram at data of size size to the host address address at port port. | |
qint64 | writeDatagram (const QByteArray &datagram, const QHostAddress &host, quint16 port) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sends the datagram datagram to the host address host and at port port. | |
Public Member Functions inherited from QAbstractSocket | |
QAbstractSocket (SocketType socketType, QObject *parent) | |
Creates a new abstract socket of type socketType. | |
virtual | ~QAbstractSocket () |
Destroys the socket. | |
virtual void | resume () |
PauseModes | pauseMode () const |
void | setPauseMode (PauseModes pauseMode) |
virtual bool | bind (const QHostAddress &address, quint16 port=0, BindMode mode=DefaultForPlatform) |
bool | bind (QHostAddress::SpecialAddress addr, quint16 port=0, BindMode mode=DefaultForPlatform) |
bool | bind (quint16 port=0, BindMode mode=DefaultForPlatform) |
virtual void | connectToHost (const QString &hostName, quint16 port, OpenMode mode=ReadWrite, NetworkLayerProtocol protocol=AnyIPProtocol) |
Attempts to make a connection to hostName on the given port. | |
void | connectToHost (const QHostAddress &address, quint16 port, OpenMode mode=ReadWrite) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Attempts to make a connection to address on port port. | |
virtual void | disconnectFromHost () |
Attempts to close the socket. | |
bool | isValid () const |
Returns true if the socket is valid and ready for use; otherwise returns false . | |
qint64 | bytesAvailable () const override |
Returns the number of incoming bytes that are waiting to be read. | |
qint64 | bytesToWrite () const override |
Returns the number of bytes that are waiting to be written. | |
quint16 | localPort () const |
Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0. | |
QHostAddress | localAddress () const |
Returns the host address of the local socket if available; otherwise returns QHostAddress::Null. | |
quint16 | peerPort () const |
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0. | |
QHostAddress | peerAddress () const |
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHostAddress::Null. | |
QString | peerName () const |
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() has not been called. | |
qint64 | readBufferSize () const |
Returns the size of the internal read buffer. | |
virtual void | setReadBufferSize (qint64 size) |
Sets the size of QAbstractSocket's internal read buffer to be size bytes. | |
void | abort () |
Aborts the current connection and resets the socket. | |
virtual qintptr | socketDescriptor () const |
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise returns -1. | |
virtual bool | setSocketDescriptor (qintptr socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite) |
Initializes QAbstractSocket with the native socket descriptor socketDescriptor. | |
virtual void | setSocketOption (QAbstractSocket::SocketOption option, const QVariant &value) |
virtual QVariant | socketOption (QAbstractSocket::SocketOption option) |
SocketType | socketType () const |
Returns the socket type (TCP, UDP, or other). | |
SocketState | state () const |
Returns the state of the socket. | |
SocketError | error () const |
Returns the type of error that last occurred. | |
void | close () override |
Closes the I/O device for the socket and calls disconnectFromHost() to close the socket's connection. | |
bool | isSequential () const override |
\reimp | |
bool | flush () |
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. | |
virtual bool | waitForConnected (int msecs=30000) |
Waits until the socket is connected, up to msecs milliseconds. | |
bool | waitForReadyRead (int msecs=30000) override |
This function blocks until new data is available for reading and the \l{QIODevice::}{readyRead()} signal has been emitted. | |
bool | waitForBytesWritten (int msecs=30000) override |
\reimp | |
virtual bool | waitForDisconnected (int msecs=30000) |
Waits until the socket has disconnected, up to msecs milliseconds. | |
void | setProxy (const QNetworkProxy &networkProxy) |
QNetworkProxy | proxy () const |
QString | protocolTag () const |
void | setProtocolTag (const QString &tag) |
\reentrant
The QUdpSocket class provides a UDP socket.
\inmodule QtNetwork
UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn't 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 bind to an address and port using bind(), then call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
The socket emits the bytesWritten() signal every time a datagram is written to the network. If you just want to send datagrams, you don't need to call bind().
The readyRead() signal is emitted whenever datagrams arrive. In that case, hasPendingDatagrams() returns true
. Call pendingDatagramSize() to obtain the size of the first pending datagram, and readDatagram() or receiveDatagram() to read it.
Example:
QUdpSocket also supports UDP multicast. Use joinMulticastGroup() and leaveMulticastGroup() to control group membership, and QAbstractSocket::MulticastTtlOption and QAbstractSocket::MulticastLoopbackOption to set the TTL and loopback socket options. Use setMulticastInterface() to control the outgoing interface for multicast datagrams, and multicastInterface() to query it.
With QUdpSocket, you can also establish a virtual connection to a UDP server using connectToHost() and then use read() and write() to exchange datagrams without specifying the receiver for each datagram.
The \l{broadcastsender}{Broadcast Sender}, \l{broadcastreceiver}{Broadcast Receiver}, \l{multicastsender}{Multicast Sender}, and \l{multicastreceiver}{Multicast Receiver} examples illustrate how to use QUdpSocket in applications.
Definition at line 21 of file qudpsocket.h.
|
explicit |
Creates a QUdpSocket object.
parent is passed to the QObject constructor.
Definition at line 122 of file qudpsocket.cpp.
|
virtual |
Destroys the socket, closing the connection if necessary.
Definition at line 133 of file qudpsocket.cpp.
bool QUdpSocket::hasPendingDatagrams | ( | ) | const |
Returns true
if at least one datagram is waiting to be read; otherwise returns false
.
Definition at line 267 of file qudpsocket.cpp.
bool QUdpSocket::joinMulticastGroup | ( | const QHostAddress & | groupAddress | ) |
Joins the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState, otherwise an error occurs.
Note that if you are attempting to join an IPv4 group, your socket must not be bound using IPv6 (or in dual mode, using QHostAddress::Any). You must use QHostAddress::AnyIPv4 instead.
This function returns true
if successful; otherwise it returns false
and sets the socket error accordingly.
Definition at line 159 of file qudpsocket.cpp.
bool QUdpSocket::joinMulticastGroup | ( | const QHostAddress & | groupAddress, |
const QNetworkInterface & | iface ) |
Joins the multicast group address groupAddress on the interface iface.
Definition at line 173 of file qudpsocket.cpp.
bool QUdpSocket::leaveMulticastGroup | ( | const QHostAddress & | groupAddress | ) |
Leaves the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState, otherwise an error occurs.
This function returns true
if successful; otherwise it returns false
and sets the socket error accordingly.
Definition at line 196 of file qudpsocket.cpp.
bool QUdpSocket::leaveMulticastGroup | ( | const QHostAddress & | groupAddress, |
const QNetworkInterface & | iface ) |
Leaves the multicast group specified by groupAddress on the interface iface.
Definition at line 213 of file qudpsocket.cpp.
QNetworkInterface QUdpSocket::multicastInterface | ( | ) | const |
Returns the interface for the outgoing interface for multicast datagrams. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. If no interface has been previously set, this function returns an invalid QNetworkInterface. The socket must be in BoundState, otherwise an invalid QNetworkInterface is returned.
Definition at line 232 of file qudpsocket.cpp.
qint64 QUdpSocket::pendingDatagramSize | ( | ) | const |
Returns the size of the first pending UDP datagram.
If there is no datagram available, this function returns -1.
Definition at line 279 of file qudpsocket.cpp.
qint64 QUdpSocket::readDatagram | ( | char * | data, |
qint64 | maxSize, | ||
QHostAddress * | address = nullptr, | ||
quint16 * | port = nullptr ) |
Receives a datagram no larger than maxSize bytes and stores it in data.
The sender's host address and port is stored in address and *port (unless the pointers are \nullptr).
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 loss of data, call pendingDatagramSize() to determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded.
Definition at line 460 of file qudpsocket.cpp.
QNetworkDatagram QUdpSocket::receiveDatagram | ( | qint64 | maxSize = -1 | ) |
Receives a datagram no larger than maxSize bytes and returns it in the QNetworkDatagram object, along with the sender's host address and port. If possible, this function will also try to determine the datagram's destination address, port, and the number of hop counts at reception time.
On failure, returns a QNetworkDatagram that reports \l {QNetworkDatagram::isValid()}{not valid}.
If maxSize is too small, the rest of the datagram will be lost. If maxSize is 0, the datagram will be discarded. If maxSize is -1 (the default), this function will attempt to read the entire datagram.
Definition at line 416 of file qudpsocket.cpp.
void QUdpSocket::setMulticastInterface | ( | const QNetworkInterface & | iface | ) |
Sets the outgoing interface for multicast datagrams to the interface iface. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. The socket must be in BoundState, otherwise this function does nothing.
Definition at line 249 of file qudpsocket.cpp.
qint64 QUdpSocket::writeDatagram | ( | const char * | data, |
qint64 | size, | ||
const QHostAddress & | address, | ||
quint16 | port ) |
Sends the datagram at data of size size to the host address address at 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.
Sending datagrams larger than 512 bytes is in general disadvised, as even if they are sent successfully, they are likely to be fragmented by the IP layer before arriving at their final destination.
Definition at line 306 of file qudpsocket.cpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sends the datagram datagram to the host address host and at port port.
The function returns the number of bytes sent if it succeeded or -1 if it encountered an error.
Definition at line 54 of file qudpsocket.h.
qint64 QUdpSocket::writeDatagram | ( | const QNetworkDatagram & | datagram | ) |
Sends the datagram datagram to the host address and port numbers contained in datagram, using the network interface and hop count limits also set there. If the destination address and port numbers are unset, this function will send to the address that was passed to connectToHost().
If the destination address is IPv6 with a non-empty \l{QHostAddress::scopeId()}{scope id} but differs from the interface index in datagram, it is undefined which interface the operating system will choose to send on.
The function returns the number of bytes sent if it succeeded or -1 if it encountered an error.
Definition at line 371 of file qudpsocket.cpp.