我有一个使用 QUdpSocket 广播心跳消息的应用程序:
mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(QHostAddress::Broadcast, clsMainWnd::mscuint16Port);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdh()));
mscuint16Port 是:
const quint16 clsMainWnd::mscuint16Port(8081);
发送消息的代码:
const QByteArray carybytData(/*My message*/);
qint64 int64Written(mpsckUDP->writeDatagram(carybytData, QHostAddress::LocalHost, clsMainWnd::mscuint16Port));
int64Written 在每次调用 writeDatagram 后被赋值为 47。在我在同一系统上运行的客户端应用程序中:
mpsckUDP = new QUdpSocket(this);
uint uintPort(8081);
mpsckUDP->bind(QHostAddress::LocalHost, uintPort);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));
插槽:
void clsMainWnd::onUDPdataRdy() {
QHostAddress objSender;
QByteArray arybytData;
quint16 uint16Port;
arybytData.resize(mpsckUDP->pendingDatagramSize());
mpsckUDP->readDatagram(arybytData.data(), arybytData.size(), &objSender, &uint16Port);
...
}
我在同一系统上的客户端应用程序上启动了两个实例,问题是只有第一个实例正在接收 UDP 广播,另一个没有接收到任何东西......
目的是拥有许多客户端和单个广播应用程序。