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