Skip to content

Instantly share code, notes, and snippets.

@wateroot
Created June 13, 2014 03:44
Show Gist options
  • Save wateroot/a70b2a33188d9c4f91f5 to your computer and use it in GitHub Desktop.
Save wateroot/a70b2a33188d9c4f91f5 to your computer and use it in GitHub Desktop.
GetIpByHostName
int GetIpByHostName(char szHost)
{
struct hostent *rmtHost = NULL;
struct in_addr stAddr = {0};
if (NULL == szHost) {
Log("GetIpByHostName input null.\n");
return -1;
}
rmtHost = gethostbyname(szHost);
if (NULL == rmtHost) {
Log("gethostbyname fail. erron=[%d].\n", GetLastError());
return -1;
}
if (AF_INET == remoteHost->h_addrtype)
{
while (remoteHost->h_addr_list[i] != 0) {
addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment