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.