gethostbyname, gethostbyaddr(原来百度叫shifen "十分?")
还以为程序错了,ping百度也是 www.a.shifen.com
#include <unistd.h> #include <netdb.h> #include <sys/socket.h> #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { struct hostent *info; int i; if((info = gethostbyname(argv[1])) == NULL) { if((info = gethostbyaddr(argv[1], strlen(argv[1]), AF_INET)) == NULL) { printf("error : ./gethostby name|ip\n"); return 0; } } printf("h_name = %s\n", info->h_name); for(i = 0; info->h_aliases[i] != NULL; ++i) printf("h_aliases[%d] = %s\n", i, info->h_aliases[i]); printf("h_addrtype = %d\n", info->h_addrtype); printf("h_length = %d\n", info->h_length); for(i = 0; info->h_addr_list[i] != NULL; ++i) printf("h_addr_list[%d] = %s\n", i, inet_ntoa(*(struct in_addr *)(info->h_addr_list[i]))); return 0; }