-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostent.cpp
39 lines (33 loc) · 1.04 KB
/
hostent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Created by Administrator on 2017/3/22.
//
#include "unp.h"
#include <netdb.h>
int main(int argc,char **argv){
char *ptr,**pptr;
char str[MAX_BUFF_SIZE] = {0};
struct hostent *hptr;
while(--argc > 0){
ptr = *++argv;
if((hptr = gethostbyname(ptr)) == NULL){
err_msg("gethostbyname error for host:%s:%s",ptr,hstrerror(h_errno));
continue;
}
std::cout << "officaial hostname " << hptr->h_name << std::endl;
for (pptr = hptr->h_aliases; pptr != NULL ; ++pptr) {
std::cout << "\talias:"<<*pptr<<std::endl;
}
switch (hptr->h_addrtype){
case AF_INET:
pptr = hptr->h_addr_list;
for(;pptr != NULL;++pptr){
std::cout << "\taddress:"<<inet_ntop(AF_INET,*pptr,str,sizeof(str))<<std::endl;
}
break;
default:
err_ret("unknown address type");
break;
}
}
return 0;
}