6.0.0-rc4Source

QNetworkDatagram Class

The QNetworkDatagram class represents a UDP datagram. More…

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

List of all members, including inherited members

Public Functions

QNetworkDatagram()
QNetworkDatagram(const QByteArray &data, const QHostAddress &destinationAddress = QHostAddress(), quint16 port = 0)
boolisValid() const
boolisNull() const
QHostAddresssenderAddress() const
QHostAddressdestinationAddress() const
intsenderPort() const
intdestinationPort() const
voidsetSender(const QHostAddress &address, quint16 port = 0)
voidsetDestination(const QHostAddress &address, quint16 port)
inthopLimit() const
voidsetHopLimit(int count)
uintinterfaceIndex() const
voidsetInterfaceIndex(uint index)
QByteArraydata() const
voidsetData(const QByteArray &data)
voidclear()
QNetworkDatagrammakeReply(const QByteArray &payload) const

Detailed Description

A QNetworkDatagram holds the payload of a datagram together with its sender and destination addresses and ports. QUdpSocket::receiveDatagram() returns one, and QUdpSocket::writeDatagram() sends one:

QNetworkDatagram d = socket.receiveDatagram();
socket.writeDatagram(d.makeReply("ack"));

See also QUdpSocket and QHostAddress.

Differences from Qt

The class is not implicitly shared, so copying a datagram copies its addresses (the QByteArray payload still shares its data). There is no swap() and isNull() is simply the negation of isValid().

Member Function Documentation

QNetworkDatagram::QNetworkDatagram()

Constructs an empty, invalid datagram.

QNetworkDatagram::QNetworkDatagram(const QByteArray &data, const QHostAddress &destinationAddress = QHostAddress(), quint16 port = 0)

Constructs a datagram containing data, to be sent to destinationAddress on port.

void QNetworkDatagram::clear()

Resets the datagram to an empty, invalid state.

QByteArray QNetworkDatagram::data() const

Returns the payload of the datagram.

See also setData().

QHostAddress QNetworkDatagram::destinationAddress() const

Returns the address the datagram was or will be sent to.

See also setDestination() and destinationPort().

int QNetworkDatagram::destinationPort() const

Returns the destination port, or -1 if it is not set.

See also setDestination() and destinationAddress().

int QNetworkDatagram::hopLimit() const

Returns the hop limit (TTL) of the datagram, or -1 if it is not set.

See also setHopLimit().

uint QNetworkDatagram::interfaceIndex() const

Returns the index of the network interface the datagram was received on or will be sent through, or 0 if it is not set.

See also setInterfaceIndex().

bool QNetworkDatagram::isNull() const

Returns true if the datagram is not valid.

See also isValid().

bool QNetworkDatagram::isValid() const

Returns true if the datagram has data or a sender or destination address; otherwise returns false.

See also isNull().

QNetworkDatagram QNetworkDatagram::makeReply(const QByteArray &payload) const

Returns a datagram carrying payload that is addressed to this datagram's sender and appears to come from its destination.

QHostAddress QNetworkDatagram::senderAddress() const

Returns the address of the sender of the datagram.

See also setSender() and senderPort().

int QNetworkDatagram::senderPort() const

Returns the port of the sender, or -1 if it is not set.

See also setSender() and senderAddress().

void QNetworkDatagram::setData(const QByteArray &data)

Sets the payload of the datagram to data.

See also data().

void QNetworkDatagram::setDestination(const QHostAddress &address, quint16 port)

Sets the destination of the datagram to address and port.

See also destinationAddress() and destinationPort().

void QNetworkDatagram::setHopLimit(int count)

Sets the hop limit of the datagram to count.

See also hopLimit().

void QNetworkDatagram::setInterfaceIndex(uint index)

Sets the network interface index of the datagram to index.

See also interfaceIndex().

void QNetworkDatagram::setSender(const QHostAddress &address, quint16 port = 0)

Sets the sender of the datagram to address and port.

See also senderAddress() and senderPort().