QT QUdpSocket类

QT QUdpSocket类提供了处理UDP协议的能力,包括检查是否有待读取的数据、加入/离开多播组、获取多播接口、读写数据报等方法。通过pendingDatagramSize()可以确定待读数据的大小,避免数据丢失。readDatagram()和writeDatagram()用于接收和发送数据报,而receiveDatagram()则返回包含完整信息的QNetworkDatagram对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.bool hasPendingDatagrams() const
Returns true if at least one datagram is waiting to be read; otherwise returns false.

2.bool joinMulticastGroup(const QHostAddress &groupAddress)

3.bool joinMulticastGroup(const QHostAddress &groupAddress, const QNetworkInterface &iface)

4.bool leaveMulticastGroup(const QHostAddress &groupAddress)

5.bool leaveMulticastGroup(const QHostAddress &groupAddress, const QNetworkInterface &iface)

6.QNetworkInterface multicastInterface() const

7.qint64 pendingDatagramSize() const
Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.

8.qint64 readDatagram(char *data, qint64 maxSize, QHostAddress *address = Q_NULLPTR, quint16 *port = Q_NULLPTR)
Receives a datagram no larger than maxSize bytes and stores it in data. The sender’s host address and port is stored in *address and *port (unless the pointers are 0);
Returns the size of the datagram on success; otherwise returns -1;
If maxSize is too small, the rest of the datagram will be lost. To avoid loss of data, call pendingDatagramSize() to determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded;

9.QNetworkDatagram receiveDatagram(qint64 maxSize = -1)
Receives a datagram no larger than maxSize bytes and returns it in the QNetworkDatagram object, along with the sender’s host address and port. If possible, this function will also try to determine the datagram’s destination address, port, and the number of hop counts at reception time;
On failure, returns a QNetworkDatagram that reports not valid;
If maxSize is too small, the rest of the datagram will be lost. If maxSize is 0, the datagram will be discarded. If maxSize is -1 (the default), this function will attempt to read the entire datagram.

10.void setMulticastInterface(const QNetworkInterface &iface)

11.qint64 writeDatagram(const char *data, qint64 size, const QHostAddress &address, quint16 port)

12.qint64 writeDatagram(const QNetworkDatagram &datagram)

13.qint64 writeDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值