QUdpSocket Class
The QUdpSocket class provides a UDP socket. More…
| Header | #include <QUdpSocket> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
| Inherits | QAbstractSocket |
List of all members, including inherited members
Public Functions
| QUdpSocket(QObject *parent = nullptr) | |
| ~QUdpSocket() | |
| bool | hasPendingDatagrams() const |
| qint64 | pendingDatagramSize() const |
| qint64 | readDatagram(char *data, qint64 maxSize, QHostAddress *address = nullptr, quint16 *port = nullptr) |
| QNetworkDatagram | receiveDatagram(qint64 maxSize = -1) |
| qint64 | writeDatagram(const char *data, qint64 size, const QHostAddress &address, quint16 port) |
| qint64 | writeDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port) |
| qint64 | writeDatagram(const QNetworkDatagram &datagram) |
| bool | joinMulticastGroup(const QHostAddress &groupAddress) |
| bool | leaveMulticastGroup(const QHostAddress &groupAddress) |
Reimplemented Public Functions
| qint64 | bytesAvailable() const |
| bool | canReadLine() const |
Reimplemented Protected Functions
Detailed Description
UDP is a message-oriented, unreliable protocol. Bind the socket with bind() to receive, then read datagrams when readyRead() is emitted:
QUdpSocket socket;
socket.bind(QHostAddress::Any, 4000);
QObject::connect(&socket, &QUdpSocket::readyRead, [&] {
while (socket.hasPendingDatagrams()) {
QNetworkDatagram d = socket.receiveDatagram();
socket.writeDatagram(d.makeReply("ack"));
}
});
A datagram can be sent without binding; the socket binds to an ephemeral port on first use. After connectToHost() the plain write() functions send to the connected peer.
See also QAbstractSocket, QTcpSocket and QNetworkDatagram.
Differences from Qt
The multicast functions take no interface argument; groups are joined on the default interface. There is no multicastInterface(), setMulticastInterface() or QNetworkInterface support. pauseMode and proxy support are not provided.
Member Function Documentation
QUdpSocket::QUdpSocket(QObject *parent = nullptr)
Creates a UDP socket with the given parent.
QUdpSocket::~QUdpSocket()
Destroys the socket, closing it.
qint64 QUdpSocket::bytesAvailable() const
Reimplements QAbstractSocket::bytesAvailable().
Returns the size of the first pending datagram, or 0 if there is none.
bool QUdpSocket::canReadLine() const
Reimplements QAbstractSocket::canReadLine().
Returns false; datagrams are not line oriented.
bool QUdpSocket::hasPendingDatagrams() const
Returns true if at least one datagram is waiting to be read; otherwise returns false.
See also pendingDatagramSize() and readDatagram().
bool QUdpSocket::joinMulticastGroup(const QHostAddress &groupAddress)
Joins the multicast group groupAddress on the default interface. Returns true on success; returns false if the socket is not bound, groupAddress is not a multicast address, or the system refuses.
See also leaveMulticastGroup().
bool QUdpSocket::leaveMulticastGroup(const QHostAddress &groupAddress)
Leaves the multicast group groupAddress. Returns true on success; otherwise returns false.
See also joinMulticastGroup().
qint64 QUdpSocket::pendingDatagramSize() const
Returns the size of the first pending datagram, or -1 if there is none. A zero-length datagram yields 0.
See also hasPendingDatagrams() and readDatagram().
qint64 QUdpSocket::readData(char *data, qint64 maxSize)
Reimplements QAbstractSocket::readData().
Reads one datagram into data, up to maxSize bytes. Returns the number of bytes read; returns 0 if no datagram is available.
qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *address = nullptr, quint16 *port = nullptr)
Receives a datagram of no more than maxSize bytes into data and returns its size, or -1 on failure. Bytes beyond maxSize are discarded. If address and port are not null, they are set to the sender's address and port.
See also receiveDatagram() and pendingDatagramSize().
QNetworkDatagram QUdpSocket::receiveDatagram(qint64 maxSize = -1)
Receives the next pending datagram and returns it with its payload, sender and destination (the socket's local address and port) filled in. At most maxSize bytes are kept; a negative value keeps the whole datagram. Returns an invalid datagram if none is pending or an error occurs.
See also readDatagram() and hasPendingDatagrams().
qint64 QUdpSocket::writeData(const char *data, qint64 maxSize)
Reimplements QAbstractSocket::writeData().
Sends maxSize bytes from data to the connected peer. Returns the number of bytes sent, or -1 with OperationError if the socket is not connected.
qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddress &address, quint16 port)
Sends the datagram at data of size bytes to address on port. Returns the number of bytes sent, or -1 on failure. If the socket is not yet bound, it is bound to an ephemeral port first.
See also readDatagram().
qint64 QUdpSocket::writeDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port)
This is an overloaded function.
Sends the contents of datagram to host on port.
qint64 QUdpSocket::writeDatagram(const QNetworkDatagram &datagram)
This is an overloaded function.
Sends datagram to its destination address and port. Returns the number of bytes sent, or -1 on failure.