I use next way and it work correct always for me
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
{
for(int i=0; ;i++)
{
char *ipadr=lh->h_addr_list[i];
if(ipadr)
{
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks