内容表

上一话题

QNetworkCookieJar

下一话题

QNetworkDiskCache

QNetworkDatagram

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

Inheritance diagram of PySide2.QtNetwork.QNetworkDatagram

New in version 5.8.

概要

函数

详细描述

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

  • 负载数据;

  • 发送器地址和端口号;

  • 目的地地址和端口号;

  • the remaining hop count limit (on IPv4, this field is usually called “time to live” - TTL);

  • 接收或发送数据报的网络接口索引。

QUdpSocket 将试着在所有操作系统尽可能匹配常见行为,但在某些操作系统无法获得以上所有元数据。无法设置数据报元数据当发送时采用 writeDatagram() will be silently discarded.

当接待时, 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.

当发送时, 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 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).

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 scopeId() property and matches the 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.

特征支持

Some features of QNetworkDatagram are not supported in all operating systems. Only the address and ports of the remote host (sender in received packets and destination for outgoing packets) are supported in all systems. On most operating systems, the other features are supported only for IPv6. Software should check at runtime whether the rest could be determined for IPv4 addresses.

当前特征支持如下所示:

操作系统

本地地址

跳跃计数

接口索引

FreeBSD

支持

支持

仅 IPv6

Linux

支持

支持

支持

OS X

支持

支持

仅 IPv6

其它 Unix 支持 RFC 3542

仅 IPv6

仅 IPv6

仅 IPv6

Windows (桌面)

支持

支持

支持

Windows RT

不支持

不支持

不支持

class QNetworkDatagram

QNetworkDatagram(data[, destinationAddress=QHostAddress()[, port=0]])

QNetworkDatagram(other)

param port

quint16

param other

QNetworkDatagram

param destinationAddress

QHostAddress

param data

QByteArray

创建 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, writeDatagram() will attempt to send the datagram to the address last associated with, by using connectToHost() .

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

PySide2.QtNetwork.QNetworkDatagram. clear ( )

清零负载数据和元数据在此 QNetworkDatagram 对象,将它们重置到其默认值。

PySide2.QtNetwork.QNetworkDatagram. data ( )
返回类型

QByteArray

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 可能为空。

另请参阅

setData()

PySide2.QtNetwork.QNetworkDatagram. destinationAddress ( )
返回类型

QHostAddress

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 isNull() .

PySide2.QtNetwork.QNetworkDatagram. destinationPort ( )
返回类型

int

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.

PySide2.QtNetwork.QNetworkDatagram. hopLimit ( )
返回类型

int

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.

另请参阅

setHopLimit()

PySide2.QtNetwork.QNetworkDatagram. interfaceIndex ( )
返回类型

uint

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 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.

PySide2.QtNetwork.QNetworkDatagram. isNull ( )
返回类型

bool

返回 true,若此 QNetworkDatagram object is null. This function is the opposite of isValid() .

PySide2.QtNetwork.QNetworkDatagram. isValid ( )
返回类型

bool

返回 true,若此 QNetworkDatagram object is valid. A valid QNetworkDatagram object contains at least one sender or receiver address. Valid datagrams can contain empty payloads.

PySide2.QtNetwork.QNetworkDatagram. makeReply ( payload )
参数

payload QByteArray

返回类型

QNetworkDatagram

PySide2.QtNetwork.QNetworkDatagram. senderAddress ( )
返回类型

QHostAddress

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 isNull() .

PySide2.QtNetwork.QNetworkDatagram. senderPort ( )
返回类型

int

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.

PySide2.QtNetwork.QNetworkDatagram. setData ( data )
参数

data QByteArray

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 .

另请参阅

data()

PySide2.QtNetwork.QNetworkDatagram. setDestination ( address , 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 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.

PySide2.QtNetwork.QNetworkDatagram. setHopLimit ( count )
参数

count int

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.

另请参阅

hopLimit()

PySide2.QtNetwork.QNetworkDatagram. setInterfaceIndex ( index )
参数

index uint

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 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 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.

PySide2.QtNetwork.QNetworkDatagram. setSender ( address [ , 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 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 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.

PySide2.QtNetwork.QNetworkDatagram. swap ( other )
参数

other QNetworkDatagram

交换此实例采用 other .