Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to find the right socket



  • Hey guys,

    I have the following code:

    com_PingTest::com_PingTest(QList<QHostAddress> lIPAddresses, QObject *parent) : QObject(parent)
    {
        for(int iItr = 0; iItr < lIPAddresses.length(); iItr++) {
            QUdpSocket *sNewUDPsocket = new QUdpSocket(this);
            if(sNewUDPsocket->bind(lIPAddresses.at(iItr),1234))
                qDebug() << lIPAddresses.at(iItr).toString() << "good";
            else
                qDebug()<< lIPAddresses.at(iItr).toString() << "not good";
            connect(sNewUDPsocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
            lOfUDPSockets.append(sNewUDPsocket);
        }
    }
    

    Now, how do I know which socket has called the readyRead - function?
    Is there a possibility to get the socket descriptor of the socket that emitted the readyRead signal?

    Thanks!


  • Qt Champions 2019

    @LeoC Simple: call sender() inside your slot which returns a pointer to the sender object. Then cast the pointer to QUdpSocket.



    • add #include <functional>
    • change the readyRead() slot to readyRead(QUdpSocket* socket)
    • change connect(sNewUDPsocket,SIGNAL(readyRead()),this,SLOT(readyRead())); to connect(sNewUDPsocket,&QUdpSocket::readyRead,this,std::bind(&com_PingTest::readyRead,this,sNewUDPsocket));

Log in to reply