0

I have an application that uses QUdpSocket to broadcast a heartbeat message:

mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(QHostAddress::Broadcast, clsMainWnd::mscuint16Port);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdh()));

mscuint16Port is:

const quint16 clsMainWnd::mscuint16Port(8081);

The code that sends a message:

const QByteArray carybytData(/*My message*/);
qint64 int64Written(mpsckUDP->writeDatagram(carybytData, QHostAddress::LocalHost, clsMainWnd::mscuint16Port));

int64Written is assigned 47 after each call to writeDatagram. In my client applications which run on the same system:

mpsckUDP = new QUdpSocket(this);
uint uintPort(8081);
mpsckUDP->bind(QHostAddress::LocalHost, uintPort);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));

The slot:

void clsMainWnd::onUDPdataRdy() {
    QHostAddress objSender;
    QByteArray arybytData;
    quint16 uint16Port;
    arybytData.resize(mpsckUDP->pendingDatagramSize());
    mpsckUDP->readDatagram(arybytData.data(), arybytData.size(), &objSender, &uint16Port);
    ...
}

I launch two instances on the client application on the same system the issue is that only the first instance is receiving the UDP broadcast, the other isn't receiving anything...

The intention is to have many clients and a single broadcasting application.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • That doesn't look like a broadcast, it looks like you are sending to `127.0.0.1` which is not a broadcast address. – Ben Voigt Mar 01 '22 at 16:38
  • @BenVoigt, I want to set-up so one application broadcasts to many listening / client applications, all running on the same system. – SPlatten Mar 01 '22 at 16:39
  • You also aren't checking the return code from `bind()`. More than one client binding to `127.0.0.1:8081` should fail. – Ben Voigt Mar 01 '22 at 16:40
  • @BenVoigt, thank you, so what is the correct way to set-up so one application can talk to multiples using UDP? – SPlatten Mar 01 '22 at 16:41
  • What you can do is bind one client to `127.0.0.2:8081`, one to `127.0.0.3:8081` etc and then send to `127.255.255.255:8081` Be sure to use IOCTL to enable broadcasting from the sending socket. – Ben Voigt Mar 01 '22 at 16:42
  • BTW I recommend you get this working first with BSD sockets functions and add Qt classes only when you have everything working and want to use signals/slots. Qt obscures the real socket error messages because it tries to force all its I/O classes to conform to a single generic interface. – Ben Voigt Mar 01 '22 at 16:44
  • @BenVoigt, thanks again, I will try this tomorrow. – SPlatten Mar 01 '22 at 16:59
  • @BenVoigt, I've modified my code so now the main application broadcasts on 127.255.255.255:8081, the clients use different addresses 127.0.0.1:8081 and 127.0.0.2:8081, but still only one of them gets the message, just searched for IOCTL, but don't see it mentioned on QUdpSocket page. – SPlatten Mar 02 '22 at 14:09
  • @BenVoigt, problem solved now... thanks – SPlatten Mar 02 '22 at 14:52
  • 1
    Excellent, please share it by writing an answer to your own question. – Ben Voigt Mar 02 '22 at 14:57

1 Answers1

0

Main application:

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()));

Broadcast logic:

qint64 int64Written(mpsckUDP->writeDatagram(carybytData, QHostAddress::Broadcast, clsMainWnd::mscuint16Port));

In the client applications:

mpsckUDP = new QUdpSocket(this);
mpsckUDP->bind(uintPort, QUdpSocket::ShareAddress);
QObject::connect(mpsckUDP, SIGNAL(readyRead()), this, SLOT(onUDPdataRdy()));

Now all is good!

SPlatten
  • 5,334
  • 11
  • 57
  • 128