如何使用c-ares将IPparsing为主机?

这是我迄今为止所做的。 它编译,但它试图运行它segfaults。

#include <iostream> #include <netdb.h> #include <arpa/inet.h> #include <ares.h> void dns_callback (void* arg, int status, int timeouts, struct hostent* host) { std::cout << host->h_name << "\n"; } int main(int argc, char **argv) { struct in_addr ip; char *arg; inet_aton(argv[1], &ip); ares_channel channel; ares_gethostbyaddr(channel, &ip, 4, AF_INET, dns_callback, arg); sleep(15); return 0; } 

Solutions Collecting From Web of "如何使用c-ares将IPparsing为主机?"

您至少需要在使用之前初始化 ares_channel

  if(ares_init(&channel) != ARES_SUCCESS) { //handle error } 

您还需要一个事件循环来处理ares文件描述符上的事件,并调用ares_process来处理这些事件(更常见的情况是,将这些事件集成到应用程序的事件循环中)。没有什么神奇的东西,它不使用线程做简单的异步处理,调用sleep(15); 不让自己跑在“背景”

您的回调也应检查status变量,如果查找失败,则无法访问host->h_name

一个完整的例子变成:

 #include <time.h> #include <iostream> #include <netdb.h> #include <arpa/inet.h> #include <ares.h> void dns_callback (void* arg, int status, int timeouts, struct hostent* host) { if(status == ARES_SUCCESS) std::cout << host->h_name << "\n"; else std::cout << "lookup failed: " << status << '\n'; } void main_loop(ares_channel &channel) { int nfds, count; fd_set readers, writers; timeval tv, *tvp; while (1) { FD_ZERO(&readers); FD_ZERO(&writers); nfds = ares_fds(channel, &readers, &writers); if (nfds == 0) break; tvp = ares_timeout(channel, NULL, &tv); count = select(nfds, &readers, &writers, NULL, tvp); ares_process(channel, &readers, &writers); } } int main(int argc, char **argv) { struct in_addr ip; int res; if(argc < 2 ) { std::cout << "usage: " << argv[0] << " ip.address\n"; return 1; } inet_aton(argv[1], &ip); ares_channel channel; if((res = ares_init(&channel)) != ARES_SUCCESS) { std::cout << "ares feiled: " << res << '\n'; return 1; } ares_gethostbyaddr(channel, &ip, sizeof ip, AF_INET, dns_callback, NULL); main_loop(channel); return 0; } 
  $ g ++ -Wall test_ares.cpp -lcares
  $ ./a.out 8.8.8.8
 google-public-dns-a.google.com