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. QUdpSockets and Windows 10

QUdpSockets and Windows 10

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 3.9k Views 3 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #6

    Oh, good point. So, do I need to return control to the event loop before I close and delete the socket(s)?

    S 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Oh, good point. So, do I need to return control to the event loop before I close and delete the socket(s)?

      S Offline
      S Offline
      shaan7
      wrote on last edited by
      #7

      @mzimmers you should delete the socket only after it has finished sending the packets. This should work-

      connect(m_sock, &QUdpSocket::bytesWritten, m_sock, &QObject::deleteLater)

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #8

        @shaan7 thanks for that. I need to determine which interface in the list is the one I should use. When I write to the "correct" interface, I'll get a response on the socket. Probably won't on the other.

        It appears that I can't do this in a loop, since the read signal doesn't get delivered. How best to know which of my attempts was the successful one?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #9

          Hi,

          You can create a QMap that contains that information and look it up before deleting the socket.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          mzimmersM 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You can create a QMap that contains that information and look it up before deleting the socket.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #10

            @SGaist I don't follow you...can you give a little more detail?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #11
              QMap<QUdpSocket *, QNetworkInterface> _socketInterfaceMap;
              

              Then in your loop you can store the relation between the socket and the network interface it sent data to.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #12

                I must be extra-dense today; I'm still not connecting the dots here.

                So, for each item in the interface list, I create a socket, bind, connect, and send out an inquiry. I get a response from one of them, signalling my read() slot. How do I know which socket sent the readyRead()?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  You use the object oriented modularity breaker QObject::sender method.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  mzimmersM 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    You use the object oriented modularity breaker QObject::sender method.

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #14

                    @SGaist OK, so sender() returns an object (pointer). How do I translate that into a QUdpSocket pointer to use as a key in my QMap?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      qobject_cast is your friend.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #16

                        OK...the sends are working, but the recv() slot isn't getting triggered. I wonder if it's due to my reuse of the slot pointer here:

                            qnil = QNetworkInterface::allInterfaces();
                            for (it = qnil.begin(); it != qnil.end(); ++it)
                            {
                                sock = new QUdpSocket(this);
                                m_socketInterfaceMap.insert(sock, *it);
                                qDebug() << it->humanReadableName();
                                QObject::connect(sock, &QUdpSocket::readyRead, this, &UdpSocket::recv);
                                sock->bind(m_addrRecv, MCAST_PORT, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint);
                                sock->setMulticastInterface(*it);
                                sock->setSocketOption(QAbstractSocket::MulticastLoopbackOption, 0);
                                m_sock = sock;
                                send(str);
                            }
                            return;
                        

                        I thought this would be legit, but...should I have a vector of these sockets instead of reusing the pointer?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          Why are you connecting the readyRead signal ? Didn't you want to use bytesWritten ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on last edited by mzimmers
                            #18

                            No, I want the signal for when the remote device responds to my send.

                            I realize I was making the code unnecessarily complicated; here's my current version:

                                // create the socket, bind to it and set some options.
                                m_sock = new QUdpSocket(this);
                            
                                rc = m_sock->bind(m_addrRecv, MCAST_PORT, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint);
                                if (rc == false)
                                {
                                    cout << m_sock->errorString().toStdString() << endl;
                                    exit(1);
                                }
                            
                                m_sock->setSocketOption(QAbstractSocket::MulticastLoopbackOption, 0);
                            
                                // join multicast group.
                                rc = m_sock->joinMulticastGroup(m_addrSend);
                                if (rc == false)
                                {
                                    cout << m_sock->errorString().toStdString() << endl;
                                    exit(1);
                                }
                            
                                QObject::connect(m_sock, &QUdpSocket::readyRead, this, &UdpSocket::recv);
                                QObject::connect(m_sock, &QUdpSocket::disconnected, this, &UdpSocket::reconnect);
                            
                                qnil = QNetworkInterface::allInterfaces();
                                for (it = qnil.begin(); it != qnil.end(); ++it)
                                {
                                    type = it->type();
                                    if (type == QNetworkInterface::Ethernet)
                                    {
                                        //m_socketInterfaceMap.insert(m_sock, *it);
                                        qDebug() << it->humanReadableName() << type;
                                        m_sock->setMulticastInterface(*it);
                                        send(str);
                                    }
                                }
                            

                            My concern now is that I'm not getting any hit on my recv() slot. Once I get that working, I can work out the matter of determining which interface was used, using the technique you described above. EDIT: I simplified it further, by only writing to the interface I know is correct, and I still don't get a recv() trigger. I think I must have messed up something in my multicast setup.

                            EDIT 2: I found the problem -- evidently, even if you choose an interface for a socket, when you call joinMulticastGroup, you still have to specify the interface, or the OS will choose one for you. With the change below, I'm now getting my recv() slot.

                                for (it = qnil.begin(); it != qnil.end(); ++it)
                                {
                                    type = it->type();
                            //        if (type == QNetworkInterface::Ethernet)
                                    if (it->humanReadableName() == "Ethernet 3")
                                    {
                                        //m_socketInterfaceMap.insert(m_sock, *it);
                                        qDebug() << it->humanReadableName() << type;
                                        m_sock->setMulticastInterface(*it);
                                        // join multicast group.
                                        rc = m_sock->joinMulticastGroup(m_addrSend, *it);
                            

                            Now I just have to use that stuff that SGaist suggested above to determine which interface supplied the response.

                            1 Reply Last reply
                            0

                            • Login

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