Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to find the right socket
Forum Updated to NodeBB v4.3 + New Features

how to find the right socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 451 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LeoCL Offline
    LeoCL Offline
    LeoC
    wrote on last edited by
    #1

    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!

    jsulmJ 1 Reply Last reply
    0
    • LeoCL LeoC

      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!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3
        • 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));

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved