QUdpSocket 广播不适用于多个客户端
QUdpSocket broadcast not working to more than one client
我有一个使用 QUdpSocket 广播心跳消息的应用程序:
mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(QHostAddress::Broadcast, clsMainWnd::mscuint16Port);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdh()));
mscuint16端口为:
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 广播,另一个没有接收到任何东西...
目的是拥有多个客户端和一个广播应用程序。
主要应用:
mpsckUDP = new QUdpSocket(this);
//Connection only required if you are going to receive data back on UDP
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));
广播逻辑:
qint64 int64Written(mpsckUDP->writeDatagram(carybytData, QHostAddress::Broadcast, clsMainWnd::mscuint16Port));
在客户端应用程序中:
mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(uintPort, QUdpSocket::ShareAddress);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));
现在一切都很好!
我有一个使用 QUdpSocket 广播心跳消息的应用程序:
mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(QHostAddress::Broadcast, clsMainWnd::mscuint16Port);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdh()));
mscuint16端口为:
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 广播,另一个没有接收到任何东西...
目的是拥有多个客户端和一个广播应用程序。
主要应用:
mpsckUDP = new QUdpSocket(this);
//Connection only required if you are going to receive data back on UDP
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));
广播逻辑:
qint64 int64Written(mpsckUDP->writeDatagram(carybytData, QHostAddress::Broadcast, clsMainWnd::mscuint16Port));
在客户端应用程序中:
mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(uintPort, QUdpSocket::ShareAddress);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));
现在一切都很好!