Results 1 to 12 of 12

Thread: How to get local IP

Hybrid View

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

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.

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

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

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

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    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
  •  
Qt is a trademark of The Qt Company.