QT QNetworkDatagram类

来源:互联网 发布:hao123网络连接错误 编辑:程序博客网 时间:2024/06/09 15:01

The QNetworkDatagram class provides the data and metadata of a UDP datagram.
QNetworkDatagram类提供UDP数据报的数据和元数据。
QNetworkDatagram可以与QUdpSocket类一起使用,以表示UDP数据报中包含的完整信息。

1.QNetworkDatagram()
2.QNetworkDatagram::QNetworkDatagram(const QByteArray &data, const QHostAddress &destinationAddress = QHostAddress(), quint16 port = 0)
data:发送数据
destinationAddress:目的IP
port:目的端口号

3.void QNetworkDatagram::clear()
Clears the payload data and metadata in this QNetworkDatagram object, resetting them to their default values.

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

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

6.int QNetworkDatagram::destinationPort() const
同上
If no destination address was associated with this datagram, this function returns -1.

7.bool QNetworkDatagram::isNull() const
*Returns true if this QNetworkDatagram object is null. This function is the opposite of isValid().

8.QNetworkDatagram QNetworkDatagram::makeReply(const QByteArray &data) const
*Creates a new QNetworkDatagram representing a reply to this incoming datagram and sets the payload data to data.(创建一个新的QNetworkDatagram,表示对该传入数据报的回复,通过这个函数可以直接知道发送者的信息,所以回复它只需将要回复的内容作为此函数的参数即可)

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

9.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.(本地地址)

10.int QNetworkDatagram::senderPort() const

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

12void QNetworkDatagram::setDestination(const QHostAddress &address, quint16 port)
将与该数据报关联的目标地址设置为地址和端口号端口。 目的地址和端口号通常由QUdpSocket在接收时设置,因此无需在接收到的数据报上调用此功能。
对于传出数据报,此功能可用于设置数据报应发送到的地址。 它可以是用于与对等体通信的单播地址或广播或多播地址发送到一组设备。

13.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.
*there’s no need to call this function on a received datagram.
*If left unset, the operating system will choose the most appropriate address to use given the destination in question.

原创粉丝点击