Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    5
    9
    11926
    Loading More Posts
    • 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
      kalster last edited by

      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 Reply Quote 0
      • F
        fluca1978 last edited by

        Are you talking about the NAT of the router?

        1 Reply Last reply Reply Quote 0
        • A
          andre last edited by

          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 Reply Quote 0
          • K
            kalster last edited by

            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 Reply Quote 0
            • R
              rich last edited by

              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 Reply Quote 0
              • S
                ShadowMoses last edited by

                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 Reply Quote 0
                • K
                  kalster last edited by

                  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 Reply Quote 0
                  • S
                    ShadowMoses last edited by

                    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 Reply Quote 0
                    • K
                      kalster last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post