Finding local and public IP address with Qt?
-
Hello,
how exactly am I supposed to get my local IP address with Qt? my public IP address? The following gets a list of IP addresses, but I don't know what to do with it afterward:
auto list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
Thanks in advance.
-
Hi
It returns a list.
QList<QHostAddress> list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
for (int var = 0; var < list.size(); ++var) {
qDebug() << list[var].XXXXXX();
}
XXXXX being what you are interested in; -
@Pippin
Its a list of QHostAddress objects.
http://doc.qt.io/qt-5.5/qhostaddress.htmlthis size of list is how many QHostAddress there is in list.
-
I did understand that, but I was asking my question in this particular instance:
auto list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
Since the local host is my computer, why should it (or any other computer for that matter) have more than 1 IP address? Why should there be a list at all? Could you explain to me how I am supposed to get my local IP address with this list? And how I'm supposed to get my public IP address? I'm a bit confused.
-
Hi
A pc can have many interfaces. Like one for lan , one for wifi.
You could also have more than one netcard.
So that's why it's a list.
Not sure how you would get local iP from a QHostAddress.People seems to use other calls:
http://stackoverflow.com/questions/12927273/how-to-get-local-ip-address-of-a-computer-using-qtUpdate:
toString() seems to return the iP
i get
"fe80::a102:d348:5850:49c1"
"fe80::5497:3dfa:7a29:4910"
"fe80::b84a:b6cc:fe61:6662"
"192.168.1.33" <<<<<<<<<<<<<< this is my real one.
"192.168.13.1"
"192.168.195.1"Update2:
For your WAN (public IP)
please see here
https://forum.qt.io/topic/12466/solved-hostinfo-h-how-to-get-the-right-ip-address/4
the
QNetworkReply reply = accessManager->get(QNetworkRequest("http://whatismyip.org/"));
post. -
@Pippin
well there is not easy / straight way to get IP
as its not how it works.Most PC have multiple interfaces (and hence ips)
and none can tell which is the correct one in just a single call.For external IP, these is no way to know except to look it up like
linked showed.Network is inherently complex and getting IP is small part of using the network classes among the
1000 other things it also must do. -
@mrjj It doesn't seem that complex for the people running the SFML: see their page.
Both the local and public addresses are only a static function away with the SFML. I wasn't expecting things to get over-complicated without this library. I'm probably going to use it for network purposes after all.
-
Have you tried it ?
It most likely just return the first found.
Anyway, Qt is much, much bigger than SFML so you cannot expect
all things to be as neatly wrapped up as SFML.But why don't you just use SFML if it suits better?
-
@mrjj I do think I'm about to use the SFML. I initially wanted to use Qt as much as possible so that I don't have to link anything non-Qt to my project. I agree, though, with your other point: I've noticed how practical and extensive Qt can get, as opposed to the SFML. It's just sad to see that the SFML is sometimes much easier to use for very straightforward issues.