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. [SOLVED] hostinfo.h: how to get the right ip address?

[SOLVED] hostinfo.h: how to get the right ip address?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 12.9k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    below is the code that i use to get the ip address. the problem is that the ip address that i get is from the router. when i go online and check my ip address, it is different that what i get from the router. how to get the right ip address that the internet detects?

    @ info = QHostInfo::fromName( QHostInfo::localHostName() );
    ip = info.addresses();
    ui->textEdit->append(ip[0].toString());@

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      Are you talking about the NAT of the router?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        When you go online and check your IP, what you get is the IP of your modem. You most probably have a private network, with IP's like 192.178.0.x or something like that. Your modem is working as a gateway between the network of your ISP and your private network, using NAT.

        So, your code is returning the correct IP address. It is the IP address of your own machine, not that of your modem/router on the public network.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalster
          wrote on last edited by
          #4

          the problem is that the 192.X.X.X ip does not resolve to domain. is there a way to get an ip for NAT?

          below is the code that i use to try to resolve my ip to domain. is there another way to do this?

          mainwindow.cpp
          @#include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include "dialog.h"
          #include "QHostInfo"

          QHostInfo info;
          QList<QHostAddress> ip;

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

          ipLookup();
          

          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::ipLookup(){

          info = QHostInfo::fromName( QHostInfo::localHostName()  );
          ip = info.addresses();
          
          // print ip address
          ui->textEdit->append(ip[0].toString());
          
          QHostInfo::lookupHost(ip[0].toString(), this, SLOT(domainLookup(QHostInfo)));
          

          }

          void MainWindow::domainLookup(const QHostInfo &host)
          {
          if (host.error() != QHostInfo::NoError) {
          ui->textEdit->append("Lookup failed:" + host.errorString());
          return;
          }

          foreach (const QHostAddress &address, host.addresses()){
              if (ip[0].toString() == address.toString())
                  ui->textEdit->append("no reverse DNS for this IP");
              else {
                  // print domain
                  ui->textEdit->append(address.toString());
              }
          }
          

          }
          @

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rich
            wrote on last edited by
            #5

            There is no real way to get the external address, that's kind of the point of NAT. This isn't a problem with your code, or with Qt, it's just how address translation works. You would either need to use a router specific way of finding the external address (eg. accessing it's web gui, or querying it via SNMP), or use an external service.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              ShadowMoses
              wrote on last edited by
              #6

              You could always use QNetworkAccessManager to grab data from whatismyip.org. That page just gives you your external IP address and nothing more..

              You could do something like so.
              @void MainWindow::getIP()
              {
              QNetworkReply reply = accessManager->get(QNetworkRequest("http://whatismyip.org/"));
              connect(accessManager, SIGNAL(finished(QNetworkReply
              )), SLOT(assignIP(QNetworkReply*)));
              }

              void MainWindow::assignIP(QNetworkReply *reply)
              {
              if(reply->error() == QNetworkReply::NoError)
              {
              ipAddress = reply->readAll();
              }
              else
              {
              QMessageBox::information(this, "Network error", "Could not get your external IP address. Your interwebs seem to be down.");
              }
              reply->deleteLater();
              }@

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on last edited by
                #7

                thank you ShadowMoses for the code. do you also have code for resolve to domain because my code does not resolve to domain for some reason?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  ShadowMoses
                  wrote on last edited by
                  #8

                  After you obtain the external IP through the method above you can just use QHostInfo like so.
                  @QHostInfo info = QHostInfo::fromName(ipAddress);
                  QString nameAddress = info.hostName();@

                  Just remember that is the blocking way of doing things. If you want you can do it with slots as well.

                  Something like this should work.
                  @QHostInfo::lookupHost(ipAddress, this, SLOT(assignHostName(QHostInfo)));

                  MainWindow::assignHostName(QHostInfo info)
                  {
                  //some error checking
                  hostAddress = info.hostName();
                  }@

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on last edited by
                    #9

                    thank you ShadowMoses. you have been lots of help :)

                    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