域名转换成IP地址
#include <string.h> #include <errno.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #define MAXLINE 1024 int main(int argc, char **argv) { struct addrinfo *p, *listp, hints; char buf[MAXLINE]; int rc, flags; if(argc!=2) { fprintf(stderr, "usage: %s <domain name>\n", argv[0]); exit(0); } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family=AF_INET; hints.ai_socktype=SOCK_STREAM; if((rc=getaddrinfo(argv[1], "ntp", &hints, &listp)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(rc)); exit(1); } flags=NI_NUMERICHOST; for(p=listp;p;p=p->ai_next) { if((rc=getnameinfo(p->ai_addr, p->ai_addrlen, buf, MAXLINE, NULL, 0, flags)) != 0) { fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc)); exit(1); } printf("%s\n", buf); } freeaddrinfo(listp); exit(0); }