Results 1 to 12 of 12

Thread: How to get local IP

  1. #1
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to get local IP

    Hi there! I was trying to get local IP address.

    Qt Code:
    1. QHostInfo info;
    2. QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
    3. QList<QHostAddress> l= info.addresses();
    4. for(int i=0; i<l.count(); i++) {
    5. qDebug()<<l[i].toString();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    but it returned only 127.0.0.1

    I also tried

    Qt Code:
    1. QTcpServer *server = new QTcpServer(this);
    2. socket->listen();
    3. socket->serverAddress()
    To copy to clipboard, switch view to plain text mode 

    But this one returned QHostAddress( "0.0.0.0" )

    Any ideas how to get local ip address?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get local IP

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get local IP

    Stated from:
    QHostInfo
    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:
    Qt Code:
    1. int main( int argc, char** argv )
    2. {
    3. QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
    4. QList<QHostAddress> l= info.addresses();
    5. for(int i=0; i<l.count(); i++) {
    6. qDebug()<<l[i].toString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get local IP

    Quote Originally Posted by orb9
    Stated from:
    QHostInfo


    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:
    Qt Code:
    1. int main( int argc, char** argv )
    2. {
    3. QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
    4. QList<QHostAddress> l= info.addresses();
    5. for(int i=0; i<l.count(); i++) {
    6. qDebug()<<l[i].toString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    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"
    $
    Last edited by wysota; 15th May 2006 at 16:17.

  5. #5
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: How to get local IP

    Quote Originally Posted by wysota
    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.

    BTW: When i use
    Qt Code:
    1. QHostInfo::fromName( "localhost" );
    To copy to clipboard, switch view to plain text mode 
    it prints: 127.0.0.1

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get local IP

    Quote Originally Posted by orb9
    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.
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to get local IP

    I use next way and it work correct always for me

    Qt Code:
    1. char hostname[255];
    2. int lhnr_res = gethostname(hostname, 255);
    3.  
    4. if(lhnr_res)
    5. printf("Can't resolve hostname!\n");
    6. else
    7. printf("Host name is '%s'\n", hostname);
    8.  
    9. hostent *lh= gethostbyname(hostname);
    10.  
    11. if(!lh)
    12. printf("Can't resolve ipaddress!\n");
    13. else
    14. {
    15. QStringList ipList;
    16. for(int i=0; ;i++)
    17. {
    18. char *ipadr=lh->h_addr_list[i];
    19.  
    20. if(ipadr)
    21. {
    22. QString sIP;
    23. sIP.sprintf("%d.%d.%d.%d", (unsigned char)ipadr[0], (unsigned char)ipadr[1], (unsigned char)ipadr[2], (unsigned char)ipadr[3]);
    24. ipList.append(sIP);
    25. printf("IP addres is %s:\n", sIP.data());
    26. }
    27. else
    28. break;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    a life without programming is like an empty bottle

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get local IP

    Probably because you have the hostname set correctly. It doesn't work for me at all.

  9. #9
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get local IP

    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...

  10. #10
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to get local IP

    Quote Originally Posted by wysota
    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
    a life without programming is like an empty bottle

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get local IP

    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.

  12. #12
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to get local IP

    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..
    a life without programming is like an empty bottle

Similar Threads

  1. item local coordinate
    By dreamer in forum Qt Programming
    Replies: 6
    Last Post: 11th May 2008, 22:49
  2. Replies: 1
    Last Post: 9th May 2008, 14:49
  3. How to get and set local ip in windows xp using QT?
    By longtrue in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2008, 08:55
  4. forcing local port number from TcpSocket?
    By nbkhwjm in forum Newbie
    Replies: 6
    Last Post: 8th January 2008, 18:28
  5. Replies: 1
    Last Post: 24th September 2007, 09:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.