Last active
December 13, 2015 23:09
-
-
Save zhouhao/4989426 to your computer and use it in GitHub Desktop.
Get IP by Hostname [Using C]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<netdb.h> | |
#include<arpa/inet.h> | |
int main(int argc, char **argv) { | |
struct hostent *host; | |
char str[100]; | |
char addr[INET_ADDRSTRLEN]; | |
char **p; | |
int c; | |
while (1){ | |
c=0; | |
gets(str); | |
printf("%s\n",str); | |
if ((host=gethostbyname(str))==NULL){ | |
perror("host"); | |
break; | |
} | |
p=host->h_addr_list; //Get the first IP from the List | |
for (;*p!=NULL;p++){ | |
c++; | |
inet_ntop(AF_INET, *p, addr, INET_ADDRSTRLEN); | |
printf("%d:%s\n", c, addr); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment