2

I an writing a Programm in Qt which should discover Routers in a LAN via UPnP. This is the constructor of my class:

Discovery::Discovery(QObject *parent):QObject(parent)
{
    groupAddress = QHostAddress("239.255.255.250");
    ssdpPort = 1900;
    socket = new QUdpSocket(this);
    socket->joinMulticastGroup(groupAddress);
    connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
    discovered = new QList<DiscoveryMatch>();
}

After creating, I send out the search message:

void Discovery::discover()
{
    QByteArray Data;
    Data.append("M-SEARCH * HTTP/1.1\r\n");
    Data.append("HOST: 239.255.255.250:1900\r\n");
    Data.append("ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n");
    Data.append("MAN: \"ssdp:discover\"\r\n");
    Data.append("MX: 5\r\n\r\n");
    socket->writeDatagram(Data, groupAddress, ssdpPort);
}

If a device answered, I process the reply with the readyRead() Signal:

void Discovery::processPendingDatagrams()
{
    QByteArray buffer;
    QHostAddress sender;
    quint16 senderPort;
    while(socket->hasPendingDatagrams())
    {
        buffer.resize(socket->pendingDatagramSize());
        socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);

        qDebug() << "Message from: " << sender.toString();
        qDebug() << "Message port: " << senderPort;
        qDebug() << "Message " << buffer;
        processDatagram(buffer);
    }
}

I have 2 Routers in my Network and if I run the Programm the socket reads the Datagram from my DrayTek Router and ignores the response from my FRITZ!Box. The strange thing is, that if I run the Programm in Debug mode the socket catches both responses as I intended.

Is this a Qt Problem or do I something wrong? Thank you for reading and for any advise.

sebter
  • 46
  • 5
  • maybe it is something related to firewall exceptions? what OS are you using? – Mike Aug 18 '16 at 09:29
  • I am using OS X. I did the same thing in Java and it works perfectly. It also works as expected when I run the Qt Programm in Debug mode. If I compile and run it, it just discovers the drayTek – sebter Aug 18 '16 at 09:32
  • I have noticed that you call [`joinMulticastGroup`](https://doc.qt.io/qt-5/qudpsocket.html#joinMulticastGroup) without the socket being bound. are you sure that it ran for you like that? – Mike Aug 18 '16 at 09:47
  • yes i get the responses. But the is a good hint. I will try it with another socket and bind it before i join the multicast group – sebter Aug 18 '16 at 10:08

0 Answers0