Nominate our 2022 Qt Champions!

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);
                qDebug() << << "good";
                qDebug()<< << "not good";

    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?


  • Lifetime Qt Champion

    @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