PDA

View Full Version : How to get local IP



naresh
14th May 2006, 14:25
Hi there! I was trying to get local IP address.


QHostInfo info;
QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
QList<QHostAddress> l= info.addresses();
for(int i=0; i<l.count(); i++) {
qDebug()<<l[i].toString();
}
}

but it returned only 127.0.0.1

I also tried


QTcpServer *server = new QTcpServer(this);
socket->listen();
socket->serverAddress()

But this one returned QHostAddress( "0.0.0.0" )

Any ideas how to get local ip address?

wysota
15th May 2006, 09:46
Well... in short words, it's not possible using Qt (and not only Qt, you can't do it with plain C too). On QtForum there was a thread about it and someone (Chicken Blood?) posted a solution, but AFAIR it wasn't a pretty one.

You can do it manually by querying /sys/class/net/*/address files. They contain addresses of network interfaces.

orb9
15th May 2006, 15:54
Stated from:
QHostInfo (http://doc.trolltech.com/4.1/qhostinfo.html)


QHostInfo uses the lookup mechanisms provided by the operating system to find the IP address(es) associated with a host name, or the host name associated with an IP address.


So it *should* work.
I simply tried:


int main( int argc, char** argv )
{
QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
QList<QHostAddress> l= info.addresses();
for(int i=0; i<l.count(); i++) {
qDebug()<<l[i].toString();
}
}

and it indeed prints out my ip-adress e.g. 154.142.4.162 (not localhost ip).

This is Qt4.1.2 on Gentoo Linux.
I did not try this under Windows.

wysota
15th May 2006, 16:15
Stated from:
QHostInfo (http://doc.trolltech.com/4.1/qhostinfo.html)


So it *should* work.

Nnnnnot quite :)

If you have a proper dns mapping and properly set hostname (according to the rev dns), then it *might* work.


I simply tried:


int main( int argc, char** argv )
{
QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
QList<QHostAddress> l= info.addresses();
for(int i=0; i<l.count(); i++) {
qDebug()<<l[i].toString();
}
}

and it indeed prints out my ip-adress e.g. 154.142.4.162 (not localhost ip).


Try this on a private network, where it is doubtful that you have a dns entry. Anyway, it should print all addresses, including 127.0.0.1. I didn't find a way to do this properly on Linux (with or without Qt).


$ hostname
cz-17.55
$ ./test
"cz-17.55"
"10.12.17.55"
$ su
Password:
# hostname "foobar"
# exit
$ hostname
foobar
$ ./test
"foobar"
$

orb9
15th May 2006, 16:42
If you have a proper dns mapping and properly set hostname (according to the rev dns), then it *might* work.

Yepp, you're right. Tested in the company network with a proper dns mapping.



Anyway, it should print all addresses, including 127.0.0.1. I didn't find a way to do this properly on Linux (with or without Qt).
Should it really include 127.0.0.1? When i resolve an ip of an network machine returning the 127.0.0.1 would not be a good idea. But hey - i'm not a network guy and i might be totally wrong here. :rolleyes:

BTW: When i use


QHostInfo::fromName( "localhost" );

it prints: 127.0.0.1

wysota
15th May 2006, 16:48
Should it really include 127.0.0.1? When i resolve an ip of an network machine returning the 127.0.0.1 would not be a good idea. But hey - i'm not a network guy and i might be totally wrong here. :rolleyes:

Yes. It's a well known address, so you can always remove it from the list if you want. But it is an addres of a very usefull network interface -- many services listen on loopback only, you can't ignore it.

zlatko
17th May 2006, 13:56
I use next way and it work correct always for me :rolleyes:



char hostname[255];
int lhnr_res = gethostname(hostname, 255);

if(lhnr_res)
printf("Can't resolve hostname!\n");
else
printf("Host name is '%s'\n", hostname);

hostent *lh= gethostbyname(hostname);

if(!lh)
printf("Can't resolve ipaddress!\n");
else
{
QStringList ipList;
for(int i=0; ;i++)
{
char *ipadr=lh->h_addr_list[i];

if(ipadr)
{
QString sIP;
sIP.sprintf("%d.%d.%d.%d", (unsigned char)ipadr[0], (unsigned char)ipadr[1], (unsigned char)ipadr[2], (unsigned char)ipadr[3]);
ipList.append(sIP);
printf("IP addres is %s:\n", sIP.data());
}
else
break;
}
}

wysota
17th May 2006, 14:58
Probably because you have the hostname set correctly. It doesn't work for me at all.

naresh
17th May 2006, 15:05
well the only way i found to do this... is connecting with QTcpSocket somwhere (ie. google at port 80). After that localAddress return my IP address...

zlatko
17th May 2006, 15:12
Probably because you have the hostname set correctly. It doesn't work for me at all.
I'm not strong in network admining but explain me at what situation and why can be declare failed hostname...
You explain it in upper posts but my poor english cant give me correct answer :confused:

wysota
17th May 2006, 15:39
You rely on a gethostbyname() call. When you pass it a host name and not an address, a lookup is made and correct IP is returned. Now if it's not possible to determine an IP for a particular host, the function will fail.

Suprisingly, it doesn't even work for me if I pass "localhost" as the hostname, which should always be resolvable. Maybe there is something wrong with your code. sIP.data() simply returns an empty string for me.

zlatko
17th May 2006, 15:48
No it copy/paste code for one our program that correct cat local IP on Windows XP, Linux Suse, and today begin work under FreeBSD...
Used Qt3..