Finding out the own IP address
-
Hi there,
I have, what I think should be, a minor problem. I like to find out the ip address of the machine my application is running. While consulting Qt's documentation I have found QHostInfo, which seemed suitable for my purpose.
@
QHostInfo info;
QString qstr = info.localHostName();
qDebug() << qstr << endl;
QHostInfo info2 ( QHostInfo ::fromName ( qstr ) );
QList <QHostAddress> hostaddr = info2.addresses();
qDebug () << hostaddr << endl;
@On my machine the code above delivers a list of 6 different ip addresses. Four of them seem to be ipv6 and two ipv4 addresses.
Four of the addresses (two ipv4 and two ipv6) I could locate. An ipv4 address is the address assigned by the router to my machine. That is actually the address I am looking for. There is also an ipv6 address assigned to the same network adapter. So I can use the ipv6 alternatively, I guess.
The other pair of ipv4, ipv6 is assigned to my VirtualBox, which is currently not in use. The remaining two ipv6 addresses I do not know where they belong.
I assume that I can use only 2 of the addresses to communicate with my machine (the ipv4 or the ipv6 address, see above).
So my basic question is, how do I know which of the addresses is the address I have to use?
Is the first the choice as suggested "in the example":http://doc.qt.nokia.com/latest/qhostinfo.html#addresses ? -
If you want to communicate with your local machine, using localhost (127.0.0.1 resp. ::1) could be a solution. Most daemons etc. listen on that adress(es) too.
You could also have a look at the Qt Mobility module (you might need to install it manually). QNetworkSession::interface() seems what you need. I never used it, so this could be complete nonsense - please check first :-)
-
Take care that a machine may have anywhere between 0 to N IP addresses. There is no such thing as the IP address of a machine.
-
Thanks to both of you for your replies.
@Andre yes, there are certainly several ip addresses available on most machines. Presumably for every hw network adapter installed and possibly a couple of virtual ones.
@Volker unfortunately, the localhost is no option. I need to know how to communicate from another computer to my machine.
I followed your suggestion a bit. The good news is, it is not complete nonsense. The bad news is that it did not help yet :-(
I obtain another QList<QHostAdress> with two more entries (the localhost in ipv4 and ipv6). At least the sequence is different :-) and it is not a step back.
I think I need to analyse the entries a bit more. -
at Volker: the interface brought up a solution (see code section below).
@
QNetworkConfigurationManager mgr;
QNetworkConfiguration nconfig = mgr.defaultConfiguration();
QNetworkSession session ( nconfig );
QNetworkInterface ninter = session.interface();
// the next statement gives you a funny name on windows
qDebug() << ninter.name() << endl;
// this gives ip addresses in different sequence, but is is a static method anyhow
// (did not see in first place)
qDebug() << ninter.allAddresses() << endl;// this provides two ip addresses (1 ipv4 and 1 ipv6) at least on my machine QList<QNetworkAddressEntry> laddr = ninter.addressEntries(); for ( QList<QNetworkAddressEntry> ::const_iterator it = laddr.begin(); it != laddr.end(); ++it ) { qDebug() << it->ip() << endl; }
@
If this is really the solution I will find out in the future.
-
I tried the above solution and it failed for the below scenario:
Connect to internet using PPP adapter through USB and create a wifi adhoc network so that other computers can connect to internet using your internet connection.The problem I was facing was that I started a tcpServer on one machine and I wanted to provide the hostIp and port number (Protocol IpV4) to the machines in the local network. So this is what I did:
@QString getHostIp(){
qWarning()<<FUNCTION;
QHostInfo hostInfo = QHostInfo::fromName(QHostInfo::localHostName());
QList<QHostAddress> hostNameLookupAddressList = hostInfo.addresses();
QList<QHostAddress> interfaceAddressList = QNetworkInterface::allAddresses();
qDebug()<<FUNCTION<<"hostName lookup addresses:"<<hostNameLookupAddressList;
qDebug()<<FUNCTION<<"interface addresses:"<<interfaceAddressList;QString hostIpStr; foreach(QHostAddress addr, hostNameLookupAddressList){ if(addr.protocol() == QAbstractSocket::IPv4Protocol && interfaceAddressList.contains(addr)){ if(isLocalIp(addr)){ qDebug()<<__FUNCTION__<<addr<<" is local ip"; hostIpStr = addr.toString(); break; }else if(isLinkLocalAddress(addr)){ qDebug()<<__FUNCTION__<<addr<<" is Link Local Address"; hostIpStr = addr.toString(); } } } return hostIpStr;
}
/**
Link Local address should be in the range
169.254.1.0 to 169.254.254.255
Refer :http://en.wikipedia.org/wiki/Private_network
**/
bool isLinkLocalAddress(QHostAddress addr){
quint32 hostIpv4Addr = addr.toIPv4Address();
quint32 rangeMin = QHostAddress("169.254.1.0").toIPv4Address();
quint32 rangeMax = QHostAddress("169.254.254.255").toIPv4Address();
if(hostIpv4Addr >= rangeMin && hostIpv4Addr <= rangeMax){
return true;
}else{
return false;
}
}/**
Refer: http://en.wikipedia.org/wiki/Private_network
Private Ipv4 address ranges:- 10.0.0.0 - 10.255.255.255
- 172.16.0.0 - 172.31.255.255
- 192.168.0.0 - 192.168.255.255
*/
bool isLocalIp(QHostAddress addr){
quint32 hostIpv4Addr = addr.toIPv4Address();
quint32 range1Min = QHostAddress("10.0.0.0").toIPv4Address();
quint32 range1Max = QHostAddress("10.255.255.255").toIPv4Address();quint32 range3Min = QHostAddress("172.16.0.0").toIPv4Address(); quint32 range3Max = QHostAddress("172.31.255.255").toIPv4Address(); quint32 range2Min = QHostAddress("192.168.0.0").toIPv4Address(); quint32 range2Max = QHostAddress("192.168.255.255").toIPv4Address(); if((hostIpv4Addr >= range1Min && hostIpv4Addr <= range1Max) || (hostIpv4Addr >= range2Min && hostIpv4Addr <= range2Max) || (hostIpv4Addr >= range3Min && hostIpv4Addr <= range3Max)){ return true; }else{ return false; }
}
@ -
It's work :)
@
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) {
qDebug() << address.toString();
}
}
@