使用QUdpSocket发送数据报 [英] Using QUdpSocket to send datagrams

查看:1013
本文介绍了使用QUdpSocket发送数据报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用QUdpSocket发送数据报.以下是我正在使用的代码:

I am trying to send a datagram using QUdpSocket. The following is the code I am using:

udpSocket = new QUdpSocket(this);
QByteArray datagram = "Message";
udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454);

现在,如果我在只有一个网络适配器的计算机上运行此程序,它似乎可以正常工作.但是,如果有多个适配器,我需要能够控制用于发送数据报的适配器.我发现如果我按如下方式绑定套接字:

Now if I run this on a computer that has only one network adapter, it seems to work with no problem. However, if there are multiple adapters, I need to be able to control which is used to send the datagram. I have found that if I bind the socket as follows:

udpSocket->bind(QHostAddress("192.168.1.104"), 45454);

然后,我可以强制将数据报发送到与该IP关联的本地网络上(否则,它似乎是随机选择一个).但是,绑定"功能将套接字设置为侦听数据包,这时我真的不感兴趣.这是控制使用哪个适配器的正确方法,还是有一些更直接的方法呢?

then I can force the datagram to be sent out on the local network associated with that IP (otherwise it appears to choose one at random). However, the 'bind' function sets up the socket to listen for packets, which I am really not interested in at this point. Is this the correct way to control which adapter is used, or is there some more straightforward way to do this?

谢谢

推荐答案

您需要类似

QHostAddress myBroadcastAddress = QHostAddress("192.168.255.255");
udpSocket->writeDatagram(datagram.data(),datagram.size(), myBroadcastAddress , 45454 )

这将发送udp广播数据包.

This will send udp broadcast packets.

这篇关于使用QUdpSocket发送数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆