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