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. QUdpSocket: Picking a specific network interface
QtWS25 Last Chance

QUdpSocket: Picking a specific network interface

Scheduled Pinned Locked Moved Solved General and Desktop
qudpsocketqnetworkmulticast
15 Posts 4 Posters 4.5k 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.
  • R Offline
    R Offline
    rtavakko
    wrote on 4 Mar 2021, 19:32 last edited by
    #1

    Hi guys,

    I'm using a QUdpSocket to receive and send packets. So far everything works well using the standard code below:

    Setting up incoming messages:

    QObject::connect(UDPSocket,&QUdpSocket::readyRead,this,&Server::processData);
    
    //Unbind the socket if currently bound
    if(UDPSocket->state() == QAbstractSocket::BoundState)
      UDPSocket->close();
    
    //Bind the socket to the specified input address and port
    UDPSocket->bind(inputIPAddress,inputPort);
    

    Sending messages:

    UDPSocket->writeDatagram(bytes,outputIPAddress,outputPort);
    

    I'm having a bit of trouble picking a specific network interface to use:

    UDPSocket->setMulticastInterface(networkAdapter);
    

    I create the input / output IP addresses from a QString and don't specify the subnet.

    What is the best way to specify a network interface?

    Cheers

    K 1 Reply Last reply 5 Mar 2021, 20:22
    0
    • R rtavakko
      4 Mar 2021, 19:32

      Hi guys,

      I'm using a QUdpSocket to receive and send packets. So far everything works well using the standard code below:

      Setting up incoming messages:

      QObject::connect(UDPSocket,&QUdpSocket::readyRead,this,&Server::processData);
      
      //Unbind the socket if currently bound
      if(UDPSocket->state() == QAbstractSocket::BoundState)
        UDPSocket->close();
      
      //Bind the socket to the specified input address and port
      UDPSocket->bind(inputIPAddress,inputPort);
      

      Sending messages:

      UDPSocket->writeDatagram(bytes,outputIPAddress,outputPort);
      

      I'm having a bit of trouble picking a specific network interface to use:

      UDPSocket->setMulticastInterface(networkAdapter);
      

      I create the input / output IP addresses from a QString and don't specify the subnet.

      What is the best way to specify a network interface?

      Cheers

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 5 Mar 2021, 20:22 last edited by
      #6
          for (const QNetworkInterface & ifcurrent : QNetworkInterface::allInterfaces()) {
              if (!ifcurrent.flags().testFlag(QNetworkInterface::CanMulticast) || !ifcurrent.flags().testFlag(QNetworkInterface::IsUp))
                  continue;
      
              QList<QNetworkAddressEntry> entries = ifcurrent.addressEntries();
              for (const QNetworkAddressEntry & entry : entries)  {
                 if (!entry.ip().isGlobal())
                    continue;
      
                  // Bind the address if you'd like to use this interface & this protocol (IPv4/IPv6)
              }
          }
      

      Read and abide by the Qt Code of Conduct

      R 1 Reply Last reply 5 Mar 2021, 22:40
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 4 Mar 2021, 19:36 last edited by SGaist 3 Apr 2021, 19:37
        #2

        Hi,

        Why not use QNetworkInterface::allInterfaces and select the one matching your criteria ?

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

        R 1 Reply Last reply 4 Mar 2021, 20:28
        1
        • S SGaist
          4 Mar 2021, 19:36

          Hi,

          Why not use QNetworkInterface::allInterfaces and select the one matching your criteria ?

          R Offline
          R Offline
          rtavakko
          wrote on 4 Mar 2021, 20:28 last edited by
          #3

          @SGaist That's what I'm doing at the moment, but it doesn't seem to work as expected. For example, if I'm using a a LAN connection and set the interface to Wi-Fi, packets still go through the wired connection

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nagesh
            wrote on 5 Mar 2021, 00:56 last edited by
            #4

            @rtavakko Try to get all the network interfaces available in the system and apply the matching criteria to get the required interface. and use that interface to set for the
            setMulticastInterface call.

            QList<QNetworkInterface> listofinterfaces =QNetworkInterface::allInterfaces();
            for(auto interface:listofinterfaces)
            {
            qDebug()<<interface.humanReadableName();
            }

            R 1 Reply Last reply 5 Mar 2021, 18:32
            0
            • N nagesh
              5 Mar 2021, 00:56

              @rtavakko Try to get all the network interfaces available in the system and apply the matching criteria to get the required interface. and use that interface to set for the
              setMulticastInterface call.

              QList<QNetworkInterface> listofinterfaces =QNetworkInterface::allInterfaces();
              for(auto interface:listofinterfaces)
              {
              qDebug()<<interface.humanReadableName();
              }

              R Offline
              R Offline
              rtavakko
              wrote on 5 Mar 2021, 18:32 last edited by
              #5

              @nagesh Thanks for replying. That's what I'm doing at the moment. I filter out VM and Bluetooth devices that way and set the interface but it seems that is ignored based on the IP settings.

              I'll put together a small QT project to better show what I'm having trouble with

              1 Reply Last reply
              0
              • R rtavakko
                4 Mar 2021, 19:32

                Hi guys,

                I'm using a QUdpSocket to receive and send packets. So far everything works well using the standard code below:

                Setting up incoming messages:

                QObject::connect(UDPSocket,&QUdpSocket::readyRead,this,&Server::processData);
                
                //Unbind the socket if currently bound
                if(UDPSocket->state() == QAbstractSocket::BoundState)
                  UDPSocket->close();
                
                //Bind the socket to the specified input address and port
                UDPSocket->bind(inputIPAddress,inputPort);
                

                Sending messages:

                UDPSocket->writeDatagram(bytes,outputIPAddress,outputPort);
                

                I'm having a bit of trouble picking a specific network interface to use:

                UDPSocket->setMulticastInterface(networkAdapter);
                

                I create the input / output IP addresses from a QString and don't specify the subnet.

                What is the best way to specify a network interface?

                Cheers

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 5 Mar 2021, 20:22 last edited by
                #6
                    for (const QNetworkInterface & ifcurrent : QNetworkInterface::allInterfaces()) {
                        if (!ifcurrent.flags().testFlag(QNetworkInterface::CanMulticast) || !ifcurrent.flags().testFlag(QNetworkInterface::IsUp))
                            continue;
                
                        QList<QNetworkAddressEntry> entries = ifcurrent.addressEntries();
                        for (const QNetworkAddressEntry & entry : entries)  {
                           if (!entry.ip().isGlobal())
                              continue;
                
                            // Bind the address if you'd like to use this interface & this protocol (IPv4/IPv6)
                        }
                    }
                

                Read and abide by the Qt Code of Conduct

                R 1 Reply Last reply 5 Mar 2021, 22:40
                1
                • K kshegunov
                  5 Mar 2021, 20:22
                      for (const QNetworkInterface & ifcurrent : QNetworkInterface::allInterfaces()) {
                          if (!ifcurrent.flags().testFlag(QNetworkInterface::CanMulticast) || !ifcurrent.flags().testFlag(QNetworkInterface::IsUp))
                              continue;
                  
                          QList<QNetworkAddressEntry> entries = ifcurrent.addressEntries();
                          for (const QNetworkAddressEntry & entry : entries)  {
                             if (!entry.ip().isGlobal())
                                continue;
                  
                              // Bind the address if you'd like to use this interface & this protocol (IPv4/IPv6)
                          }
                      }
                  
                  R Offline
                  R Offline
                  rtavakko
                  wrote on 5 Mar 2021, 22:40 last edited by
                  #7

                  @kshegunov Thanks for that. So you suggest to only specify an adapter and not the IP / port and pick those settings based on the adapter?

                  I think that works for incoming settings but not for output IP / port.

                  Here is a project I put together:

                  https://github.com/rtavakko/MulticastServer

                  It seems that even though you are able to set the interface to a specific one, your IP settings override that and messages are sent and received using the best suited interface.

                  For example for outgoing messages I tried creating a packet like this:

                  QNetworkDatagram datagram;
                  
                  datagram.setData(bytes);
                  datagram.setInterfaceIndex(UDPSocket->multicastInterface().index());
                  datagram.setDestination(outputIPAddress,outputPort);
                  

                  This would mean that your packet should be sent using the interface index you provide but it still picks whichever interface the IP and port are on instead of failing to send.

                  K 1 Reply Last reply 5 Mar 2021, 23:21
                  0
                  • R rtavakko
                    5 Mar 2021, 22:40

                    @kshegunov Thanks for that. So you suggest to only specify an adapter and not the IP / port and pick those settings based on the adapter?

                    I think that works for incoming settings but not for output IP / port.

                    Here is a project I put together:

                    https://github.com/rtavakko/MulticastServer

                    It seems that even though you are able to set the interface to a specific one, your IP settings override that and messages are sent and received using the best suited interface.

                    For example for outgoing messages I tried creating a packet like this:

                    QNetworkDatagram datagram;
                    
                    datagram.setData(bytes);
                    datagram.setInterfaceIndex(UDPSocket->multicastInterface().index());
                    datagram.setDestination(outputIPAddress,outputPort);
                    

                    This would mean that your packet should be sent using the interface index you provide but it still picks whichever interface the IP and port are on instead of failing to send.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 5 Mar 2021, 23:21 last edited by
                    #8

                    I'm quoting from memory here, but I believe the interface is determined based on the IP you bound the socket to. So bind to the correct IP and it should be fine (I think).

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    2
                    • N Offline
                      N Offline
                      nagesh
                      wrote on 6 Mar 2021, 00:20 last edited by
                      #9

                      @rtavakko is Application doing unicast reception and multicast transmission?

                      Application is using single socket for reception and transmission.

                      QUdpSocket* UDPSocket;
                      

                      initialize() function in your code binds the socket to the interface for the incoming connection (input IPAddress and Port) and same is being used for the transmission.

                      Are you using multicast series for transmission ?

                      R 1 Reply Last reply 7 Mar 2021, 18:30
                      2
                      • N nagesh
                        6 Mar 2021, 00:20

                        @rtavakko is Application doing unicast reception and multicast transmission?

                        Application is using single socket for reception and transmission.

                        QUdpSocket* UDPSocket;
                        

                        initialize() function in your code binds the socket to the interface for the incoming connection (input IPAddress and Port) and same is being used for the transmission.

                        Are you using multicast series for transmission ?

                        R Offline
                        R Offline
                        rtavakko
                        wrote on 7 Mar 2021, 18:30 last edited by
                        #10

                        @nagesh I guess what I'm trying to do is multicast reception and unicast transmission. I thought that a single UDP socket can be bound to an input IP / port to receive messages and connect to a separate IP / port to send messages, is that not the case? What do you recommend as the best way to do this?

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nagesh
                          wrote on 7 Mar 2021, 23:33 last edited by
                          #11

                          @rtavakko if sockets settings are different for sending and receiving, better to have TxSocket and RxSocket seperate.

                          what I'm trying to do is multicast reception and unicast transmission
                          

                          In order receive multicast data joinMulticastGroup API call to be called with the multicast group address. (multicast series from 224.0.0.1 to 239.255.255.255)

                          but your code doesn't contain any joinMulticastGroup call

                          R 1 Reply Last reply 7 Mar 2021, 23:39
                          2
                          • N nagesh
                            7 Mar 2021, 23:33

                            @rtavakko if sockets settings are different for sending and receiving, better to have TxSocket and RxSocket seperate.

                            what I'm trying to do is multicast reception and unicast transmission
                            

                            In order receive multicast data joinMulticastGroup API call to be called with the multicast group address. (multicast series from 224.0.0.1 to 239.255.255.255)

                            but your code doesn't contain any joinMulticastGroup call

                            R Offline
                            R Offline
                            rtavakko
                            wrote on 7 Mar 2021, 23:39 last edited by
                            #12

                            @nagesh Good point. I tried making that call after I bound the socket in initialize() but it didn't make a difference in what adapter was picked. I'll try it again this week. Will I need a call to leave the multicast group when changing rx settings?

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nagesh
                              wrote on 7 Mar 2021, 23:59 last edited by nagesh 3 Jul 2021, 23:59
                              #13

                              @rtavakko if Rx previous settings was called with joinMulticastGroup then while changing the settings good practice to call leaveMulticastGroup.

                              R 1 Reply Last reply 8 Mar 2021, 00:01
                              2
                              • N nagesh
                                7 Mar 2021, 23:59

                                @rtavakko if Rx previous settings was called with joinMulticastGroup then while changing the settings good practice to call leaveMulticastGroup.

                                R Offline
                                R Offline
                                rtavakko
                                wrote on 8 Mar 2021, 00:01 last edited by
                                #14

                                @nagesh Sounds good, I'll try that and update here

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  rtavakko
                                  wrote on 23 Aug 2021, 18:40 last edited by rtavakko
                                  #15

                                  The solution that worked for me was to pick the network adapter and then look through its list of IP addresses to pick one to bind the socket to based on given criteria as suggested by @kshegunov. I've updated the example project:
                                  https://github.com/rtavakko/MulticastServer

                                  1 Reply Last reply
                                  1

                                  • Login

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