I had a similar experience all was well, where the readyRead() signal was not emitted unless I combined the interface with the joinMulticastGroup() method similar to the following example:
{
qDebug() << "MainWindow::MainWindow:: Failed bind of multicast socket";
}
if (!iface.isValid())
{
qDebug() << "MainWindow::MainWindow:: Multicast socket interface is invalid or does not exist";
}
else
{
m_udpSocket->setMulticastInterface(iface);
// NOTE: must provide iface to the following join call or else readyRead() signal will not be emitted
if (!m_udpSocket->joinMulticastGroup(groupAddress4, iface))
{
qDebug() << "MainWindow::MainWindow:: Failed to join multicast group" << groupAddress4.toString() << "on interface" << iface.humanReadableName();
}
else
{
qDebug() << "MainWindow::MainWindow:: Successfully joined multicast group" << groupAddress4.toString() << "on interface" << m_udpSocket->multicastInterface();
printNetworkInterfaceInfo(iface);
}
auto fd = m_udpSocket->socketDescriptor();
if (fd != -1)
{
// Make connection for new data received
if (!connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())))
{
qDebug() << "MainWindow::MainWindow:: Failed to connect multicast socket readyRead() to handler";
}
else
{
qDebug() << "MainWindow::MainWindow:: Connected multicast socket readyRead() to handler";
#ifdef UDP_WRITE_TEST
QByteArray data
("THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S TAIL");
QNetworkDatagram datagram(data, groupAddress4, UDP_DEST_PORT);
m_udpSocket->writeDatagram(datagram);
#endif
}
}
else
{
qDebug("MainWindow::MainWindow:: Failed to obtain UDP socket descriptor, network sensing is not possible");
}
}
m_udpSocket = new(std::nothrow)QUdpSocket();
if (!m_udpSocket->bind(QHostAddress::AnyIPv4, UDP_DEST_PORT, QUdpSocket::ShareAddress))
{
qDebug() << "MainWindow::MainWindow:: Failed bind of multicast socket";
}
QNetworkInterface iface = getNetworkInterfaceByAddress("10.100.0.200");
if (!iface.isValid())
{
qDebug() << "MainWindow::MainWindow:: Multicast socket interface is invalid or does not exist";
}
else
{
m_udpSocket->setMulticastInterface(iface);
QHostAddress groupAddress4(QStringLiteral("224.1.2.8"));
// NOTE: must provide iface to the following join call or else readyRead() signal will not be emitted
if (!m_udpSocket->joinMulticastGroup(groupAddress4, iface))
{
qDebug() << "MainWindow::MainWindow:: Failed to join multicast group" << groupAddress4.toString() << "on interface" << iface.humanReadableName();
}
else
{
qDebug() << "MainWindow::MainWindow:: Successfully joined multicast group" << groupAddress4.toString() << "on interface" << m_udpSocket->multicastInterface();
printNetworkInterfaceInfo(iface);
}
auto fd = m_udpSocket->socketDescriptor();
if (fd != -1)
{
// Make connection for new data received
if (!connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())))
{
qDebug() << "MainWindow::MainWindow:: Failed to connect multicast socket readyRead() to handler";
}
else
{
qDebug() << "MainWindow::MainWindow:: Connected multicast socket readyRead() to handler";
#ifdef UDP_WRITE_TEST
QByteArray data("THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S TAIL");
QNetworkDatagram datagram(data, groupAddress4, UDP_DEST_PORT);
m_udpSocket->writeDatagram(datagram);
#endif
}
}
else
{
qDebug("MainWindow::MainWindow:: Failed to obtain UDP socket descriptor, network sensing is not possible");
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks