6.0.0-rc4Source

QUdpSocket Class

The QUdpSocket class provides a UDP socket. More…

Header#include <QUdpSocket>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes
InheritsQAbstractSocket

List of all members, including inherited members

Public Functions

QUdpSocket(QObject *parent = nullptr)
~QUdpSocket()
boolhasPendingDatagrams() const
qint64pendingDatagramSize() const
qint64readDatagram(char *data, qint64 maxSize, QHostAddress *address = nullptr, quint16 *port = nullptr)
QNetworkDatagramreceiveDatagram(qint64 maxSize = -1)
qint64writeDatagram(const char *data, qint64 size, const QHostAddress &address, quint16 port)
qint64writeDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port)
qint64writeDatagram(const QNetworkDatagram &datagram)
booljoinMulticastGroup(const QHostAddress &groupAddress)
boolleaveMulticastGroup(const QHostAddress &groupAddress)

Reimplemented Public Functions

qint64bytesAvailable() const
boolcanReadLine() const

Reimplemented Protected Functions

qint64readData(char *data, qint64 maxSize)
qint64writeData(const char *data, qint64 maxSize)

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

[explicit] QUdpSocket::QUdpSocket(QObject *parent = nullptr)

Creates a UDP socket with the given parent.

[override] QUdpSocket::~QUdpSocket()

Destroys the socket, closing it.

[override] qint64 QUdpSocket::bytesAvailable() const

Reimplements QAbstractSocket::bytesAvailable().

Returns the size of the first pending datagram, or 0 if there is none.

[override] 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().

[override, protected] 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().

[override, protected] 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.