Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QNetworkDatagram Class Reference

The QNetworkDatagram class provides the data and metadata of a UDP datagram. More...

#include <qnetworkdatagram.h>

+ Collaboration diagram for QNetworkDatagram:

Public Member Functions

 QNetworkDatagram ()
 Creates a QNetworkDatagram object with no payload data and undefined destination address.
 
 QNetworkDatagram (const QByteArray &data, const QHostAddress &destinationAddress=QHostAddress(), quint16 port=0)
 Creates a QNetworkDatagram object and sets data as the payload data, along with destinationAddress and port as the destination address of the datagram.
 
 QNetworkDatagram (const QNetworkDatagram &other)
 Creates a copy of the other datagram, including the payload and metadata.
 
QNetworkDatagramoperator= (const QNetworkDatagram &other)
 Copies the other datagram, including the payload and metadata.
 
 ~QNetworkDatagram ()
 
 QNetworkDatagram (QNetworkDatagram &&other) noexcept
 
QNetworkDatagramoperator= (QNetworkDatagram &&other) noexcept
 
void swap (QNetworkDatagram &other) noexcept
 Swaps this instance with other.
 
void clear ()
 Clears the payload data and metadata in this QNetworkDatagram object, resetting them to their default values.
 
bool isValid () const
 Returns true if this QNetworkDatagram object is valid.
 
bool isNull () const
 Returns true if this QNetworkDatagram object is null.
 
uint interfaceIndex () const
 Returns the interface index this datagram is associated with.
 
void setInterfaceIndex (uint index)
 Sets the interface index this datagram is associated with to index.
 
QHostAddress senderAddress () const
 Returns the sender address associated with this datagram.
 
QHostAddress destinationAddress () const
 Returns the destination address associated with this datagram.
 
int senderPort () const
 Returns the port number of the sender associated with this datagram.
 
int destinationPort () const
 Returns the port number of the destination associated with this datagram.
 
void setSender (const QHostAddress &address, quint16 port=0)
 Sets the sender address associated with this datagram to be the address address and port number port.
 
void setDestination (const QHostAddress &address, quint16 port)
 Sets the destination address associated with this datagram to be the address address and port number port.
 
int hopLimit () const
 Returns the hop count limit associated with this datagram.
 
void setHopLimit (int count)
 Sets the hop count limit associated with this datagram to count.
 
QByteArray data () const
 Returns the data payload of this datagram.
 
void setData (const QByteArray &data)
 Sets the data payload of this datagram to data.
 
QNetworkDatagram makeReply (const QByteArray &paylaod) const
 Creates a new QNetworkDatagram representing a reply to this incoming datagram and sets the payload data to payload.
 

Friends

class QUdpSocket
 
class QSctpSocket
 

Detailed Description

The QNetworkDatagram class provides the data and metadata of a UDP datagram.

Since
5.8

\inmodule QtNetwork \reentrant

QNetworkDatagram can be used with the \l QUdpSocket class to represent the full information contained in a UDP (User Datagram Protocol) datagram. QNetworkDatagram encapsulates the following information of a datagram: \list

  • the payload data;
  • the sender address and port number;
  • the destination address and port number;
  • the remaining hop count limit (on IPv4, this field is usually called "time to live" - TTL);
  • the network interface index the datagram was received on or to be sent on. \endlist

QUdpSocket will try to match a common behavior as much as possible on all operating systems, but not all of the metadata above can be obtained in some operating systems. Metadata that cannot be set on the datagram when sending with QUdpSocket::writeDatagram() will be silently discarded.

Upon reception, the senderAddress() and senderPort() properties contain the address and port of the peer that sent the datagram, while destinationAddress() and destinationPort() contain the target that was contained in the datagram. That is usually an address local to the current machine, but it can also be an IPv4 broadcast address (such as "255.255.255.255") or an IPv4 or IPv6 multicast address. Applications may find it useful to determine if the datagram was sent specifically to this machine via unicast addressing or whether it was sent to multiple destinations.

When sending, the senderAddress() and senderPort() should contain the local address to be used when sending. The sender address must be an address that is assigned to this machine, which can be obtained using \l{QNetworkInterface}, and the port number must be the port number that the socket is bound to. Either field can be left unset and will be filled in by the operating system with default values. The destinationAddress() and destinationPort() fields may be set to a target address different from the one the UDP socket is currently associated with.

Usually, when sending a datagram in reply to a datagram previously received, one will set the destinationAddress() to be the senderAddress() of the incoming datagram and similarly for the port numbers. To facilitate this common process, QNetworkDatagram provides the function makeReply().

The hopCount() function contains, for a received datagram, the remaining hop count limit for the packet. When sending, it contains the hop count limit to be set. Most protocols will leave this value set to the default and let the operating system decide on the best value to be used. Multicasting over IPv4 often uses this field to indicate the scope of the multicast group (link-local, local to an organization or global).

The interfaceIndex() function contains the index of the operating system's interface that received the packet. This value is the same one that can be set on a QHostAddress::scopeId() property and matches the QNetworkInterface::index() property. When sending packets to global addresses, it is not necessary to set the interface index as the operating system will choose the correct one using the system routing table. This property is important when sending datagrams to link-local destinations, whether unicast or multicast.

Definition at line 17 of file qnetworkdatagram.h.

Constructor & Destructor Documentation

◆ QNetworkDatagram() [1/4]

QNetworkDatagram::QNetworkDatagram ( )

Creates a QNetworkDatagram object with no payload data and undefined destination address.

The payload can be modified by using setData() and the destination address can be set with setDestination().

If the destination address is left undefined, QUdpSocket::writeDatagram() will attempt to send the datagram to the address last associated with, by using QUdpSocket::connectToHost().

Definition at line 110 of file qnetworkdatagram.cpp.

◆ QNetworkDatagram() [2/4]

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

Creates a QNetworkDatagram object and sets data as the payload data, along with destinationAddress and port as the destination address of the datagram.

Definition at line 119 of file qnetworkdatagram.cpp.

◆ QNetworkDatagram() [3/4]

QNetworkDatagram::QNetworkDatagram ( const QNetworkDatagram & other)

Creates a copy of the other datagram, including the payload and metadata.

To create a datagram suitable for sending in a reply, use QNetworkDatagram::makeReply();

Definition at line 129 of file qnetworkdatagram.cpp.

◆ ~QNetworkDatagram()

QNetworkDatagram::~QNetworkDatagram ( )
inline

Definition at line 25 of file qnetworkdatagram.h.

References d.

◆ QNetworkDatagram() [4/4]

QNetworkDatagram::QNetworkDatagram ( QNetworkDatagram && other)
inlinenoexcept

Definition at line 28 of file qnetworkdatagram.h.

References other().

+ Here is the call graph for this function:

Member Function Documentation

◆ clear()

void QNetworkDatagram::clear ( )

Clears the payload data and metadata in this QNetworkDatagram object, resetting them to their default values.

Definition at line 155 of file qnetworkdatagram.cpp.

References QByteArray::clear(), QHostAddress::clear(), QNetworkDatagramPrivate::data, QIpPacketHeader::destinationAddress, QNetworkDatagramPrivate::header, QIpPacketHeader::hopLimit, QIpPacketHeader::ifindex, and QIpPacketHeader::senderAddress.

+ Here is the call graph for this function:

◆ data()

QByteArray QNetworkDatagram::data ( ) const

Returns the data payload of this datagram.

For a datagram received from the network, it contains the payload of the datagram. For an outgoing datagram, it is the datagram to be sent.

Note that datagrams can be transmitted with no data, so the returned QByteArray may be empty.

See also
setData()

Definition at line 390 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::data.

Referenced by setData().

+ Here is the caller graph for this function:

◆ destinationAddress()

QHostAddress QNetworkDatagram::destinationAddress ( ) const

Returns the destination address associated with this datagram.

For a datagram received from the network, it is the address the peer node sent the datagram to, which can either be a local address of this machine or a multicast or broadcast address. For an outgoing datagrams, it is the address the datagram should be sent to.

If no destination address was set on this datagram, the returned object will report true to QHostAddress::isNull().

See also
senderAddress(), destinationPort(), setDestination()

Definition at line 209 of file qnetworkdatagram.cpp.

References QIpPacketHeader::destinationAddress, and QNetworkDatagramPrivate::header.

◆ destinationPort()

int QNetworkDatagram::destinationPort ( ) const

Returns the port number of the destination associated with this datagram.

For a datagram received from the network, it is the local port number that the peer node sent the datagram to. For an outgoing datagram, it is the peer port the datagram should be sent to.

If no destination address was associated with this datagram, this function returns -1.

See also
destinationAddress(), senderPort(), setDestination()

Definition at line 242 of file qnetworkdatagram.cpp.

References QIpPacketHeader::destinationAddress, QIpPacketHeader::destinationPort, QNetworkDatagramPrivate::header, QHostAddress::protocol(), and QAbstractSocket::UnknownNetworkLayerProtocol.

+ Here is the call graph for this function:

◆ hopLimit()

int QNetworkDatagram::hopLimit ( ) const

Returns the hop count limit associated with this datagram.

The hop count limit is the number of nodes that are allowed to forward the IP packet before it expires and an error is sent back to the sender of the datagram. In IPv4, this value is usually known as "time to live" (TTL).

If this datagram was received from the network, this is the remaining hop count of the datagram after reception and was decremented by 1 by each node that forwarded the packet. A value of -1 indicates that the hop limit count not be obtained.

If this is an outgoing datagram, this is the value to be set in the IP header upon sending. A value of -1 indicates the operating system should choose the value.

See also
setHopLimit()

Definition at line 308 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, and QIpPacketHeader::hopLimit.

◆ interfaceIndex()

uint QNetworkDatagram::interfaceIndex ( ) const

Returns the interface index this datagram is associated with.

The interface index is a positive number that uniquely identifies the network interface in the operating system. This number matches the value returned by QNetworkInterface::index() for the interface.

If this datagram was received from the network, this is the index of the interface that the packet was received from. If this is an outgoing datagram, this is the index of the interface that the datagram should be sent on.

A value of 0 indicates that the interface index is unknown.

See also
setInterfaceIndex()

Definition at line 349 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, and QIpPacketHeader::ifindex.

◆ isNull()

QNetworkDatagram::isNull ( ) const
inline

Returns true if this QNetworkDatagram object is null.

This function is the opposite of isValid().

Definition at line 39 of file qnetworkdatagram.h.

◆ isValid()

bool QNetworkDatagram::isValid ( ) const

Returns true if this QNetworkDatagram object is valid.

A valid QNetworkDatagram object contains at least one sender or receiver address. Valid datagrams can contain empty payloads.

Definition at line 175 of file qnetworkdatagram.cpp.

References QIpPacketHeader::destinationAddress, QNetworkDatagramPrivate::header, QHostAddress::protocol(), QIpPacketHeader::senderAddress, and QAbstractSocket::UnknownNetworkLayerProtocol.

+ Here is the call graph for this function:

◆ makeReply()

QNetworkDatagram QNetworkDatagram::makeReply ( const QByteArray & paylaod) const
inline

Creates a new QNetworkDatagram representing a reply to this incoming datagram and sets the payload data to payload.

This function is a very convenient way of responding to a datagram back to the original sender.

Example:

void Server::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
QByteArray replyData = processThePayload(datagram.data());
udpSocket->writeDatagram(datagram.makeReply(replyData));
}
}

This function is especially convenient since it will automatically copy parameters from this datagram to the new datagram as appropriate:

\list

  • this datagram's sender address and port are copied to the new datagram's destination address and port;
  • this datagram's interface index, if any, is copied to the new datagram's interface index;
  • this datagram's destination address and port are copied to the new datagram's sender address and port only if the address is IPv6 global (non-multicast) address;
  • the hop count limit on the new datagram is reset to the default (-1); \endlist

If QNetworkDatagram is modified in a future version of Qt to carry further metadata, this function will copy that metadata as appropriate.

This datagram's destination address is not copied if it is an IPv4 address because it is not possible to tell an IPv4 broadcast address apart from a regular IPv4 address without an exhaustive search of all addresses assigned to this machine. Attempting to send a datagram with the sender address equal to the broadcast address is likely to fail. However, this should not affect the communication as network interfaces with multiple IPv4 addresses are uncommon, so the address the operating system will select will likely be one the peer will understand.

Note
This function comes with both rvalue- and lvalue-reference qualifier overloads, so it is a good idea to make sure this object is rvalue, if possible, before calling makeReply, so as to make better use of move semantics. To achieve that, the example above would use:

Definition at line 64 of file qnetworkdatagram.h.

◆ operator=() [1/2]

QNetworkDatagram & QNetworkDatagram::operator= ( const QNetworkDatagram & other)

Copies the other datagram, including the payload and metadata.

To create a datagram suitable for sending in a reply, use QNetworkDatagram::makeReply();

Definition at line 145 of file qnetworkdatagram.cpp.

References other().

+ Here is the call graph for this function:

◆ operator=() [2/2]

QNetworkDatagram & QNetworkDatagram::operator= ( QNetworkDatagram && other)
inlinenoexcept

Definition at line 31 of file qnetworkdatagram.h.

References other(), and swap().

+ Here is the call graph for this function:

◆ senderAddress()

QHostAddress QNetworkDatagram::senderAddress ( ) const

Returns the sender address associated with this datagram.

For a datagram received from the network, it is the address of the peer node that sent the datagram. For an outgoing datagrams, it is the local address to be used when sending.

If no sender address was set on this datagram, the returned object will report true to QHostAddress::isNull().

See also
destinationAddress(), senderPort(), setSender()

Definition at line 192 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, and QIpPacketHeader::senderAddress.

◆ senderPort()

int QNetworkDatagram::senderPort ( ) const

Returns the port number of the sender associated with this datagram.

For a datagram received from the network, it is the port number that the peer node sent the datagram from. For an outgoing datagram, it is the local port the datagram should be sent from.

If no sender address was associated with this datagram, this function returns -1.

See also
senderAddress(), destinationPort(), setSender()

Definition at line 225 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, QHostAddress::protocol(), QIpPacketHeader::senderAddress, QIpPacketHeader::senderPort, and QAbstractSocket::UnknownNetworkLayerProtocol.

+ Here is the call graph for this function:

◆ setData()

void QNetworkDatagram::setData ( const QByteArray & data)

Sets the data payload of this datagram to data.

It is usually not necessary to call this function on received datagrams. For outgoing datagrams, this function sets the data to be sent on the network.

Since datagrams can empty, an empty QByteArray is a valid value for data.

See also
data()

Definition at line 405 of file qnetworkdatagram.cpp.

References data(), and QNetworkDatagramPrivate::data.

+ Here is the call graph for this function:

◆ setDestination()

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

Sets the destination address associated with this datagram to be the address address and port number port.

The destination address and port numbers are usually set by \l QUdpSocket upon reception, so there's no need to call this function on a received datagram.

For outgoing datagrams, this function can be used to set the address the datagram should be sent to. It can be the unicast address used to communicate with the peer or a broadcast or multicast address to send to a group of devices.

See also
QUdpSocket::writeDatagram(), destinationAddress(), destinationPort(), setSender()

Definition at line 285 of file qnetworkdatagram.cpp.

References QIpPacketHeader::destinationAddress, QIpPacketHeader::destinationPort, and QNetworkDatagramPrivate::header.

◆ setHopLimit()

void QNetworkDatagram::setHopLimit ( int count)

Sets the hop count limit associated with this datagram to count.

The hop count limit is the number of nodes that are allowed to forward the IP packet before it expires and an error is sent back to the sender of the datagram. In IPv4, this value is usually known as "time to live" (TTL).

It is usually not necessary to call this function on datagrams received from the network.

If this is an outgoing packet, this is the value to be set in the IP header upon sending. The valid range for the value is 1 to 255. This function also accepts a value of -1 to indicate that the operating system should choose the value.

See also
hopLimit()

Definition at line 329 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, and QIpPacketHeader::hopLimit.

◆ setInterfaceIndex()

void QNetworkDatagram::setInterfaceIndex ( uint index)

Sets the interface index this datagram is associated with to index.

The interface index is a positive number that uniquely identifies the network interface in the operating system. This number matches the value returned by QNetworkInterface::index() for the interface.

It is usually not necessary to call this function on datagrams received from the network.

If this is an outgoing packet, this is the index of the interface the datagram should be sent on. A value of 0 indicates that the operating system should choose the interface based on other factors.

Note that the interface index can also be set with QHostAddress::setScopeId() for IPv6 destination addresses and then with setDestination(). If the scope ID set in the destination address and index are different and neither is zero, it is undefined which interface the operating system will send the datagram on.

See also
setInterfaceIndex()

Definition at line 375 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, and QIpPacketHeader::ifindex.

◆ setSender()

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

Sets the sender address associated with this datagram to be the address address and port number port.

The sender address and port numbers are usually set by \l QUdpSocket upon reception, so there's no need to call this function on a received datagram.

For outgoing datagrams, this function can be used to set the address the datagram should carry. The address address must usually be one of the local addresses assigned to this machine, which can be obtained using \l QNetworkInterface. If left unset, the operating system will choose the most appropriate address to use given the destination in question.

The port number port must be the port number associated with the socket, if there is one. The value of 0 can be used to indicate that the operating system should choose the port number.

See also
QUdpSocket::writeDatagram(), senderAddress(), senderPort(), setDestination()

Definition at line 266 of file qnetworkdatagram.cpp.

References QNetworkDatagramPrivate::header, QIpPacketHeader::senderAddress, and QIpPacketHeader::senderPort.

◆ swap()

void QNetworkDatagram::swap ( QNetworkDatagram & other)
inlinenoexcept

Swaps this instance with other.

Definition at line 34 of file qnetworkdatagram.h.

References d, other(), and qt_ptr_swap().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QSctpSocket

friend class QSctpSocket
friend

Definition at line 71 of file qnetworkdatagram.h.

◆ QUdpSocket

friend class QUdpSocket
friend

Definition at line 70 of file qnetworkdatagram.h.


The documentation for this class was generated from the following files: