Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Network problems



  • 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);
    


  • @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;
                    }
                }
    
    


  • @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;
                    }
                }
    
    


  • mark as solved?



  • @Kent-Dorfman It is solved.


Log in to reply