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. Network problems
Forum Update on Monday, May 27th 2025

Network problems

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 338 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    My application has two command line switches,/listNI and /useNI:, when I use /listNI the result is:

    Network interfaces:
    Ethernet 2
    Ethernet
    Ethernet 3
    Local Area Connection* 1
    Local Area Connection* 2
    WiFi
    Loopback Pseudo-Interface 1
    

    When I start the application with /useNI:"Ethernet 2" I get:

    Using specified interface: Ethernet 2
    IP: 10.30.200.226 port: 30119
    

    If I start the application with /useNI:"WiFi" I get:

    Using specified interface: WiFi
    IP: 10.30.200.226 port: 30119
    

    Which to me looks wrong because if I use ipconfig -all in the command line I can see the Wireless LAN adapter WiFi has the IPv4 Address 192.168.5.52(Preferred), shouldn't the IP address issued by the adapter be in that range?

    Here is the code I wrote to scan and use the network adapter:

    QHostAddress SckServer::localAddress(QString* pstrInterface) {
        if ( pstrInterface != nullptr ) {
        //Ensure interface is empty until populated by results        
            *pstrInterface = "";
        }
        if ( SckServer::msblnOnlyLocalHost == true ) {
        //Working in local host mode
            return QHostAddress::LocalHost;
        }
        if ( SckServer::msstrInterface.isEmpty() != true ) {
        //Network interface has been specified use it!
            for( const QNetworkInterface& crInterface : QNetworkInterface::allInterfaces() ) {
                if ( crInterface.humanReadableName().compare(SckServer::msstrInterface) != 0 ) {
                    continue;
                }
                for( const QHostAddress& address : crInterface.allAddresses() ) {
                    if ( address.protocol() == QAbstractSocket::IPv4Protocol &&
                         address.isLoopback() != true ) {
                        if ( pstrInterface != nullptr ) {
                            *pstrInterface = SckServer::msstrInterface;
                        }
                        return address;
                    }
                }
            }
        }
        for( const QHostAddress& address : QNetworkInterface::allAddresses() ) {
            if ( address.protocol() == QAbstractSocket::IPv4Protocol &&
                 address.isLoopback() != true ) {
                return address;
            }
        }
        return QHostAddress::LocalHost;
    }
    

    When /useNI: is used the static member msstrInterface is set to the human readable name of the chosen adapter. I can see from the debug statement that the correct interface is being used but the IP always comes back as the same.

    The code that produces the output:

        QString strInterface;
        const QHostAddress local(localAddress(&strInterface));
        //The server is also going to listen for UDP data
        qinf() << QString("%1IP: %2, Port: %3")
                    .arg((strInterface.isEmpty() != true) ?
                            QString("Network adapter: %1, ")
                                .arg(SckServer::msstrInterface) : "")
                    .arg(local.toString())
                    .arg(SckServer::msuint16PortUDP);
    

    Kind Regards,
    Sy

    SPlattenS 1 Reply Last reply
    0
    • SPlattenS SPlatten

      My application has two command line switches,/listNI and /useNI:, when I use /listNI the result is:

      Network interfaces:
      Ethernet 2
      Ethernet
      Ethernet 3
      Local Area Connection* 1
      Local Area Connection* 2
      WiFi
      Loopback Pseudo-Interface 1
      

      When I start the application with /useNI:"Ethernet 2" I get:

      Using specified interface: Ethernet 2
      IP: 10.30.200.226 port: 30119
      

      If I start the application with /useNI:"WiFi" I get:

      Using specified interface: WiFi
      IP: 10.30.200.226 port: 30119
      

      Which to me looks wrong because if I use ipconfig -all in the command line I can see the Wireless LAN adapter WiFi has the IPv4 Address 192.168.5.52(Preferred), shouldn't the IP address issued by the adapter be in that range?

      Here is the code I wrote to scan and use the network adapter:

      QHostAddress SckServer::localAddress(QString* pstrInterface) {
          if ( pstrInterface != nullptr ) {
          //Ensure interface is empty until populated by results        
              *pstrInterface = "";
          }
          if ( SckServer::msblnOnlyLocalHost == true ) {
          //Working in local host mode
              return QHostAddress::LocalHost;
          }
          if ( SckServer::msstrInterface.isEmpty() != true ) {
          //Network interface has been specified use it!
              for( const QNetworkInterface& crInterface : QNetworkInterface::allInterfaces() ) {
                  if ( crInterface.humanReadableName().compare(SckServer::msstrInterface) != 0 ) {
                      continue;
                  }
                  for( const QHostAddress& address : crInterface.allAddresses() ) {
                      if ( address.protocol() == QAbstractSocket::IPv4Protocol &&
                           address.isLoopback() != true ) {
                          if ( pstrInterface != nullptr ) {
                              *pstrInterface = SckServer::msstrInterface;
                          }
                          return address;
                      }
                  }
              }
          }
          for( const QHostAddress& address : QNetworkInterface::allAddresses() ) {
              if ( address.protocol() == QAbstractSocket::IPv4Protocol &&
                   address.isLoopback() != true ) {
                  return address;
              }
          }
          return QHostAddress::LocalHost;
      }
      

      When /useNI: is used the static member msstrInterface is set to the human readable name of the chosen adapter. I can see from the debug statement that the correct interface is being used but the IP always comes back as the same.

      The code that produces the output:

          QString strInterface;
          const QHostAddress local(localAddress(&strInterface));
          //The server is also going to listen for UDP data
          qinf() << QString("%1IP: %2, Port: %3")
                      .arg((strInterface.isEmpty() != true) ?
                              QString("Network adapter: %1, ")
                                  .arg(SckServer::msstrInterface) : "")
                      .arg(local.toString())
                      .arg(SckServer::msuint16PortUDP);
      
      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #2

      @SPlatten, fixed by changing inner loop to:

                  for( const QNetworkAddressEntry& crAddrEntry : crInterface.addressEntries() )
                  {
                      const QHostAddress& crAddr(crAddrEntry.ip());
                      if ( crAddr.protocol() == QAbstractSocket::IPv4Protocol
                        && crAddr.isLoopback() != true )
                      {
                          if ( pstrInterface != nullptr )
                          {
                              *pstrInterface = SckServer::msstrInterface;
                          }
                          return crAddr;
                      }
                  }
      
      

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        mark as solved?

        SPlattenS 1 Reply Last reply
        0
        • Kent-DorfmanK Kent-Dorfman

          mark as solved?

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          @Kent-Dorfman It is solved.

          Kind Regards,
          Sy

          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