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. Cannot get host address from name
Forum Updated to NodeBB v4.3 + New Features

Cannot get host address from name

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.6k 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
    rackover
    wrote on last edited by
    #1

    Hi,

    I'm new to Qt. I'm trying to make a very simple console application using Qt Network.
    In this application, I want to get the host IP from its address.
    I want to get this IP in a blocking way : I am not interested in callbacks and asynchronous.

    But both ways, asynchronous QHostInfo::lookupHost(); and synchronous QHostInfo::fromName();, fail to me. I do not understand why and I would need at least one of them to work.

    Here is the code :

    const QString serverName = "lobby.faforever.com";
    QHostAddress serverAddress;
    quint16 serverPort = 8001;
    QTcpSocket *socket = nullptr;
    
    class Task : public QObject
    {
    public:
        void resolveDNS(QString DNS, QHostAddress& address) {
            QHostInfo::lookupHost(DNS, this, SLOT(finished(address, QHostInfo)));
        };
        void finished(QHostAddress& address, QHostInfo hostInfo){
            address = hostInfo.addresses()[0];
        }
    };
    
    
    int main()
    {
        qDebug() << "Program started" << endl;
    
    
        qStdOut() << "->RESOLVING ADDRESS FOR " << serverName.toUtf8() << endl;
    
        // NON-BLOCKING WAY :
        //
        // Task thisTask;
        // thisTask.resolveDNS(serverName, serverAddress);
        // while (serverAddress.isNull()){
        //    //do nothing
        // }
        //
    
        // BLOCKING WAY :
        //
        // QHostInfo servInf = QHostInfo::fromName(serverName);
        // serverAddress = servInf.addresses()[0];
        //
    
    
        qStdOut() << "->CONNECTING TO " << serverAddress.toString() << ":" << serverPort << endl;
    
        socket->connectToHost(serverAddress, serverPort);
        socket->setSocketOption(QTcpSocket::KeepAliveOption, 1);
    
        // we need to wait...
        if(socket->waitForConnected(1000))
        {
            qDebug() <<  "Connected!";
        }
       return 0;
    }
    

    The blocking way, "fromName", is returning this upon fire :

    QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()
    

    The non-blocking way, via my resolveDNS() function, returns this :

    QHostInfo::lookupHost() called with no event dispatcher
    

    What can I do ?
    Regards,

    jsulmJ Taz742T 2 Replies Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      Have you seen? http://doc.qt.io/qt-5/qdnslookup.html

      I'm sorry I don't have the effort to understand, to me it looks like you are not properly using / trying to abuse signals and slots - or something. I'd just use them in the conventional way.

      I myself would use a connection using the new syntax like:

      connect(dns, QDnsLookup::finished, this, &MyObject::handleServers);
      
      1 Reply Last reply
      0
      • R rackover

        Hi,

        I'm new to Qt. I'm trying to make a very simple console application using Qt Network.
        In this application, I want to get the host IP from its address.
        I want to get this IP in a blocking way : I am not interested in callbacks and asynchronous.

        But both ways, asynchronous QHostInfo::lookupHost(); and synchronous QHostInfo::fromName();, fail to me. I do not understand why and I would need at least one of them to work.

        Here is the code :

        const QString serverName = "lobby.faforever.com";
        QHostAddress serverAddress;
        quint16 serverPort = 8001;
        QTcpSocket *socket = nullptr;
        
        class Task : public QObject
        {
        public:
            void resolveDNS(QString DNS, QHostAddress& address) {
                QHostInfo::lookupHost(DNS, this, SLOT(finished(address, QHostInfo)));
            };
            void finished(QHostAddress& address, QHostInfo hostInfo){
                address = hostInfo.addresses()[0];
            }
        };
        
        
        int main()
        {
            qDebug() << "Program started" << endl;
        
        
            qStdOut() << "->RESOLVING ADDRESS FOR " << serverName.toUtf8() << endl;
        
            // NON-BLOCKING WAY :
            //
            // Task thisTask;
            // thisTask.resolveDNS(serverName, serverAddress);
            // while (serverAddress.isNull()){
            //    //do nothing
            // }
            //
        
            // BLOCKING WAY :
            //
            // QHostInfo servInf = QHostInfo::fromName(serverName);
            // serverAddress = servInf.addresses()[0];
            //
        
        
            qStdOut() << "->CONNECTING TO " << serverAddress.toString() << ":" << serverPort << endl;
        
            socket->connectToHost(serverAddress, serverPort);
            socket->setSocketOption(QTcpSocket::KeepAliveOption, 1);
        
            // we need to wait...
            if(socket->waitForConnected(1000))
            {
                qDebug() <<  "Connected!";
            }
           return 0;
        }
        

        The blocking way, "fromName", is returning this upon fire :

        QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()
        

        The non-blocking way, via my resolveDNS() function, returns this :

        QHostInfo::lookupHost() called with no event dispatcher
        

        What can I do ?
        Regards,

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

        @rackover This simply cannot work as you dont't have QApplication instance and you do not start event loop. Please take a look at Qt basics and examples.

        Don't do such things in event driven applications like Qt application! You block the event loop!

        while (serverAddress.isNull()){
            do nothing
        }
        

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

        1 Reply Last reply
        4
        • R rackover

          Hi,

          I'm new to Qt. I'm trying to make a very simple console application using Qt Network.
          In this application, I want to get the host IP from its address.
          I want to get this IP in a blocking way : I am not interested in callbacks and asynchronous.

          But both ways, asynchronous QHostInfo::lookupHost(); and synchronous QHostInfo::fromName();, fail to me. I do not understand why and I would need at least one of them to work.

          Here is the code :

          const QString serverName = "lobby.faforever.com";
          QHostAddress serverAddress;
          quint16 serverPort = 8001;
          QTcpSocket *socket = nullptr;
          
          class Task : public QObject
          {
          public:
              void resolveDNS(QString DNS, QHostAddress& address) {
                  QHostInfo::lookupHost(DNS, this, SLOT(finished(address, QHostInfo)));
              };
              void finished(QHostAddress& address, QHostInfo hostInfo){
                  address = hostInfo.addresses()[0];
              }
          };
          
          
          int main()
          {
              qDebug() << "Program started" << endl;
          
          
              qStdOut() << "->RESOLVING ADDRESS FOR " << serverName.toUtf8() << endl;
          
              // NON-BLOCKING WAY :
              //
              // Task thisTask;
              // thisTask.resolveDNS(serverName, serverAddress);
              // while (serverAddress.isNull()){
              //    //do nothing
              // }
              //
          
              // BLOCKING WAY :
              //
              // QHostInfo servInf = QHostInfo::fromName(serverName);
              // serverAddress = servInf.addresses()[0];
              //
          
          
              qStdOut() << "->CONNECTING TO " << serverAddress.toString() << ":" << serverPort << endl;
          
              socket->connectToHost(serverAddress, serverPort);
              socket->setSocketOption(QTcpSocket::KeepAliveOption, 1);
          
              // we need to wait...
              if(socket->waitForConnected(1000))
              {
                  qDebug() <<  "Connected!";
              }
             return 0;
          }
          

          The blocking way, "fromName", is returning this upon fire :

          QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()
          

          The non-blocking way, via my resolveDNS() function, returns this :

          QHostInfo::lookupHost() called with no event dispatcher
          

          What can I do ?
          Regards,

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by Taz742
          #4

          @rackover
          Your socket is allways null. Where is a socket = new QTcpSocket() ??
          Also

          @rackover said in Cannot get host address from name:

          class Task : public QObject
          {
          public:

          Signals and slots are available only in QObject derived classes, and a Q_OBJECT macro is needed. You forget Q_OBJECT.
          Correct Is

          class Task : public QObject
          {
               Q_OBJECT
          public:
          

          Also your finished function does not a slot:
          correct is :

          public slots:
              void finished(QHostAddress& address, QHostInfo hostInfo){
                  address = hostInfo.addresses()[0];
              }
          

          Use this example to solve your problem:

          void MainWindow::lookUp(QHostInfo info)
          {
              if (info.error()  != QHostInfo::NoError) {
                  qDebug() << info.errorString();
                  return;
              }
          
              for (int i = 0; i < info.addresses().size(); i++) {
                  qDebug() << info.addresses().at(i).toString();
              }
          }
          
          void MainWindow::on_pushButton_clicked() //on_pushButton_clicked() is a your void resolveDNS(QString DNS, QHostAddress& address)  
          {
              QHostInfo::lookupHost("qt.io", this, SLOT(lookUp(QHostInfo)));
          }
          
          private slots:
              void lookUp(QHostInfo info);
          

          Do what you want.

          1 Reply Last reply
          3
          • R Offline
            R Offline
            rackover
            wrote on last edited by
            #5

            Thank you for all your answers.

            Given the small app I want to build, the blocking synchronous way is what I need to make work.

            So I've enhanced my code a bit and deleted the other :

            //#include <QCoreApplication>
            #include <QtCore>
            #include <QtNetwork>
            
            //Debugging
            #include <QDebug>
            
            using namespace std;
            
            const QString serverName = "lobby.faforever.com";
            QHostAddress serverAddress;
            quint16 serverPort = 8001;
            string firstQuery = "{'command':'ask_session','version':'1.0','user_agent':'faf-client'}";
            
            QTcpSocket *socket = new QTcpSocket();
            
            
            QTextStream& qStdOut()
            {
                static QTextStream ts( stdout );
                return ts;
            }
            
            int main()
            {
                qDebug() << "Program started" << endl;
            
            
                qStdOut() << "->RESOLVING ADDRESS FOR " << serverName.toUtf8() << endl;
            
                // BLOCKING WAY :
                //
            
                 QHostInfo servInf = QHostInfo::fromName(serverName);
                 QList<QHostAddress> addressesList = servInf.addresses();
            
                 for (int i = 0; i < addressesList.size(); ++i) {
                     if (addressesList.at(i) != QHostAddress::LocalHost &&
                         addressesList.at(i).toIPv4Address() ) {
                         serverAddress = addressesList.at(i);
            
                         break;
                     }
                 }
                //
            
            
                qStdOut() << "->CONNECTING TO " << serverAddress.toString() << ":" << serverPort << endl;
            
                socket->connectToHost(serverAddress, serverPort);
                socket->setSocketOption(QTcpSocket::KeepAliveOption, 1);
            
                // we need to wait...
                if(socket->waitForConnected(1000))
                {
                    qDebug() <<  "Connected!";
                }
               return 0;
            }
            

            Adding " new QTcpSocket();" instead of starting the socket as a null value makes the thing work. But despite it working, the same error is still displaying :

            ->RESOLVING ADDRESS FOR lobby.faforever.com
            QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()
            ->CONNECTING TO 148.251.238.131:8001
            Connected!
            

            Why that ?
            You said the socket should be up before doing a host lookup : but how can my socket be connected to Host if I dont know host address first ?

            jsulmJ 1 Reply Last reply
            0
            • R rackover

              Thank you for all your answers.

              Given the small app I want to build, the blocking synchronous way is what I need to make work.

              So I've enhanced my code a bit and deleted the other :

              //#include <QCoreApplication>
              #include <QtCore>
              #include <QtNetwork>
              
              //Debugging
              #include <QDebug>
              
              using namespace std;
              
              const QString serverName = "lobby.faforever.com";
              QHostAddress serverAddress;
              quint16 serverPort = 8001;
              string firstQuery = "{'command':'ask_session','version':'1.0','user_agent':'faf-client'}";
              
              QTcpSocket *socket = new QTcpSocket();
              
              
              QTextStream& qStdOut()
              {
                  static QTextStream ts( stdout );
                  return ts;
              }
              
              int main()
              {
                  qDebug() << "Program started" << endl;
              
              
                  qStdOut() << "->RESOLVING ADDRESS FOR " << serverName.toUtf8() << endl;
              
                  // BLOCKING WAY :
                  //
              
                   QHostInfo servInf = QHostInfo::fromName(serverName);
                   QList<QHostAddress> addressesList = servInf.addresses();
              
                   for (int i = 0; i < addressesList.size(); ++i) {
                       if (addressesList.at(i) != QHostAddress::LocalHost &&
                           addressesList.at(i).toIPv4Address() ) {
                           serverAddress = addressesList.at(i);
              
                           break;
                       }
                   }
                  //
              
              
                  qStdOut() << "->CONNECTING TO " << serverAddress.toString() << ":" << serverPort << endl;
              
                  socket->connectToHost(serverAddress, serverPort);
                  socket->setSocketOption(QTcpSocket::KeepAliveOption, 1);
              
                  // we need to wait...
                  if(socket->waitForConnected(1000))
                  {
                      qDebug() <<  "Connected!";
                  }
                 return 0;
              }
              

              Adding " new QTcpSocket();" instead of starting the socket as a null value makes the thing work. But despite it working, the same error is still displaying :

              ->RESOLVING ADDRESS FOR lobby.faforever.com
              QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()
              ->CONNECTING TO 148.251.238.131:8001
              Connected!
              

              Why that ?
              You said the socket should be up before doing a host lookup : but how can my socket be connected to Host if I dont know host address first ?

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

              @rackover said in Cannot get host address from name:

              but how can my socket be connected to Host if I dont know host address first ?

              Using the URL?
              http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost

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

              R 1 Reply Last reply
              0
              • jsulmJ jsulm

                @rackover said in Cannot get host address from name:

                but how can my socket be connected to Host if I dont know host address first ?

                Using the URL?
                http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost

                R Offline
                R Offline
                rackover
                wrote on last edited by
                #7
                This post is deleted!
                jsulmJ 1 Reply Last reply
                0
                • R rackover

                  This post is deleted!

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

                  @rackover The host name is the URL: "lobby.faforever.com"...

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

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rackover
                    wrote on last edited by
                    #9

                    Using the servername directly, i'm still getting the same error.
                    QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()

                    Why that ?

                    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