getaddrinfo:数据库查找期间发生不可恢复的错误
getaddrinfo: A non-recoverable error occurred during a database lookup
我在 Windows 10 运行 运行以下 C 程序时从 getaddrinfo()
收到这个奇怪的错误:
https://gist.github.com/anonymous/4406cb031791afba29e5
这是调用 getaddrinfo()
的代码的相关部分:
int error;
socket_t sock;
struct sockaddr_in addr;
struct addrinfo ai_hints, *ai_result, *ai_cur;
#ifdef _WIN32
int wsa_error;
WSADATA wsa_data;
#endif
if (argc < 3) {
fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
return 1;
}
#ifdef _WIN32
wsa_error = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (wsa_error != 0) {
fprintf(stderr, "WSAStartup: %s\n",
get_error_string(wsa_error, NULL, 0));
return 2;
}
#endif
puts("Hello, World!");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
fprintf(stderr, "socket: %s\n",
get_error_string(get_socket_error(), NULL, 0));
return 3;
}
memset(&ai_hints, sizeof(ai_hints), 0);
ai_hints.ai_family = AF_UNSPEC;
ai_hints.ai_protocol = IPPROTO_TCP;
ai_hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(argv[1], argv[2], &ai_hints, &ai_result);
if (error != 0) {
fprintf(stderr, "getaddrinfo: %s\n", error);
close(sock);
return 4;
}
每次程序获得 运行 无论我通过命令行传递什么主机名和端口,它都会输出以下内容:
Hello, World!
getaddrinfo: A non-recoverable error occurred during a database lookup.
我也写了相应的服务器程序,效果很好(用telnet查过!)。
你能看出我的代码有什么问题吗?
编辑:
它似乎在 Linux 上工作正常,getaddrinfo()
没有给出任何错误,客户端能够连接到我的服务器。
万岁!我找到了!
事实证明,我在初始化 ai_hints
时搞乱了 memset()
中的参数顺序,即,而不是:
memset(&ai_hints, sizeof(ai_hints), 0);
应该是:
memset(&ai_hints, 0, sizeof(ai_hints));
太糟糕了,我没有完整阅读 getaddrinfo()
文档,它准确描述了这个错误情况:
pHints [in, optional]
A pointer to an addrinfo structure that provides hints about the type of socket the caller supports.
The ai_addrlen, ai_canonname, ai_addr, and ai_next members of the addrinfo structure pointed to by the pHints parameter must be zero or
NULL. Otherwise the GetAddrInfoEx function will fail with
WSANO_RECOVERY.
我在 Windows 10 运行 运行以下 C 程序时从 getaddrinfo()
收到这个奇怪的错误:
https://gist.github.com/anonymous/4406cb031791afba29e5
这是调用 getaddrinfo()
的代码的相关部分:
int error;
socket_t sock;
struct sockaddr_in addr;
struct addrinfo ai_hints, *ai_result, *ai_cur;
#ifdef _WIN32
int wsa_error;
WSADATA wsa_data;
#endif
if (argc < 3) {
fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
return 1;
}
#ifdef _WIN32
wsa_error = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (wsa_error != 0) {
fprintf(stderr, "WSAStartup: %s\n",
get_error_string(wsa_error, NULL, 0));
return 2;
}
#endif
puts("Hello, World!");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
fprintf(stderr, "socket: %s\n",
get_error_string(get_socket_error(), NULL, 0));
return 3;
}
memset(&ai_hints, sizeof(ai_hints), 0);
ai_hints.ai_family = AF_UNSPEC;
ai_hints.ai_protocol = IPPROTO_TCP;
ai_hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(argv[1], argv[2], &ai_hints, &ai_result);
if (error != 0) {
fprintf(stderr, "getaddrinfo: %s\n", error);
close(sock);
return 4;
}
每次程序获得 运行 无论我通过命令行传递什么主机名和端口,它都会输出以下内容:
Hello, World!
getaddrinfo: A non-recoverable error occurred during a database lookup.
我也写了相应的服务器程序,效果很好(用telnet查过!)。
你能看出我的代码有什么问题吗?
编辑:
它似乎在 Linux 上工作正常,getaddrinfo()
没有给出任何错误,客户端能够连接到我的服务器。
万岁!我找到了!
事实证明,我在初始化 ai_hints
时搞乱了 memset()
中的参数顺序,即,而不是:
memset(&ai_hints, sizeof(ai_hints), 0);
应该是:
memset(&ai_hints, 0, sizeof(ai_hints));
太糟糕了,我没有完整阅读 getaddrinfo()
文档,它准确描述了这个错误情况:
pHints [in, optional]
A pointer to an addrinfo structure that provides hints about the type of socket the caller supports.
The ai_addrlen, ai_canonname, ai_addr, and ai_next members of the addrinfo structure pointed to by the pHints parameter must be zero or NULL. Otherwise the GetAddrInfoEx function will fail with WSANO_RECOVERY.