Signals are not triggering common slot



  • Hi!

    I am using QUdpSocket from multicast. m_listUDP is a QList<QUdpSocket*>.
    Each one is connected to a common signal : connect(socketToAppend, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));

    The problem : readPendingDatagrams is never triggered. If I use a just a QUdpSocket* it is working. It seems the connection is not working cause to the QList<QUdpSocket*>.

    Maybe I forgot something?

    Classic::PrepareAndSend()
    {
        udpSocketSend = new QUdpSocket(this);
        bcast = new QHostAddress("239.255.255.250");
    
        m_listHost = QNetworkInterface::allInterfaces();
        for(int i(0); i < m_listHost.count(); i++)
        {
    
            if(m_listHost.at(i).flags().testFlag(QNetworkInterface::IsUp)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::IsRunning)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::CanBroadcast)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::CanMulticast) )
            {
                
    			//Get IP address of interface
                QNetworkInterface iface = QNetworkInterface::interfaceFromName(m_listHost.at(i).name());
                QList<QNetworkAddressEntry> entries = iface.addressEntries();
                QNetworkAddressEntry entry = entries.at(1);
                QHostAddress ip = entry.ip();
    
    	    QUdpSocket *socketToAppend = new QUdpSocket(this);
    
                socketToAppend->bind(ip, udpSocketSend->localPort());
                connect(socketToAppend, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    			
                m_listUDP.append(socketToAppend);
            }
    
        }
    
        udpSocketSend->connectToHost(*bcast, 1950);
    
        QByteArray datagram;
        datagram.append("SEARCH");
    	
        udpSocketSend->writeDatagram(datagram.data(), datagram.size(),
                                     QHostAddress::Broadcast, 1950);
    
    
    }
    
    void Classic::readPendingDatagrams()
    {
        QUdpSocket* socketToAppend = qobject_cast<QUdpSocket*>(sender());
        while (socketToAppend->hasPendingDatagrams())
        {
            QByteArray datagram;
            datagram.resize(socketToAppend->pendingDatagramSize());
            QHostAddress sender;
            quint16 senderPort;
    
            socketToAppend->readDatagram(datagram.data(), datagram.size(),
                                        &sender, &senderPort);
    
            qDebug() << datagram;
        }
    }
    

    Thank you



  • connectToHost is asynchronous, either you add udpSocketSend->waitForConnected(); after udpSocketSend->connectToHost(*bcast, 1950); or you write the datagram in a slot connected to the connected() signal of udpSocketSend

    P.S.
    if your compiler supports C++11 the sender() monster should be always avoidable:

    add the sender as argument
    void Classic::readPendingDatagrams(QUdpSocket* sender)

    and connect using a lambda:
    connect(socketToAppend, &QUdpSocket::readyRead, [socketToAppend,this]()->void{readPendingDatagrams(socketToAppend);});



  • in addition previous post, it is not issue with connect. If your loop is executing, it connect works perfectly. However issue may be other end of the UDP socket may not be sending the data at all to you end point. So readyRead() signal may not be emitted at all. Please check whether other udp end is sending the data.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.