-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <arpa/inet.h> | ||
#include <netdb.h> | ||
|
||
void error_handling(const char *message); | ||
|
||
int main(int argc,char *argv[]) | ||
{ | ||
int i; | ||
struct hostent *host; | ||
struct sockaddr_in addr; | ||
if(argc!=2){ | ||
printf("Usage : %s <IP>\n",argv[0]); | ||
exit(1); | ||
} | ||
|
||
memset(&addr,0,sizeof(addr)); | ||
addr.sin_addr.s_addr=inet_addr(argv[1]); | ||
host=gethostbyaddr((char*)&addr.sin_addr,4,AF_INET); | ||
if(!host) | ||
error_handling("gethost error"); | ||
|
||
printf("host of_name: %s \n",host->h_name); | ||
for(i=0;host->h_aliases[i];i++) | ||
printf("The %d host other name: %s\n", | ||
i+1,host->h_aliases[i]); | ||
printf("addrType: %s\n", | ||
(host->h_addrtype==AF_INET)?"AF_INET":"ipv6"); | ||
//for(i=0;host->h_addr_list[i];i++) | ||
// printf("The %d other IP: s \n", | ||
//i+1,inet_ntoa(*(struct in_addr*)host->h_addr_list[i])); | ||
|
||
//与上方相同,更容易理解 | ||
for (int i = 0; host->h_addr_list[i] != NULL; i++) { | ||
struct in_addr addr; | ||
memcpy(&addr, host->h_addr_list[i], sizeof(struct in_addr)); | ||
printf("The %d other IP: %s \n", i+1, inet_ntoa(addr)); | ||
} | ||
return 0; | ||
} | ||
|
||
void error_handling(const char *message){ | ||
fputs(message,stderr); | ||
fputc('\n',stderr); | ||
exit(1); | ||
} |