c++ - getaddrinfo() 返回错误 "The requested name is valid, but no data of the requested type was found."

标签 c++ c sockets

我在示例程序中使用 getaddrinfo 将主机名转换为地址。但它失败并出现此错误“请求的名称有效,但找不到请求类型的数据。”。

示例代码:

struct addrinfo hints, *save_res= 0, *res= 0;
int gai_rc;
char host[] = "localhost";
char port[] = "3333";

/* set hints for getaddrinfo */
ZeroMemory( &hints, sizeof(hints) );
hints.ai_protocol= IPPROTO_TCP; /* TCP connections only */
hints.ai_family= AF_UNSPEC;     /* includes: IPv4, IPv6 or hostname */
hints.ai_socktype= SOCK_STREAM;
/* Get the address information for the server using getaddrinfo() */
gai_rc= getaddrinfo(host, port, &hints, &res);
if (gai_rc != 0)
{
    printf("getaddrinfo failed with error: %d - %s\n", gai_rc, gai_strerrorA(gai_rc));
    return false;
}
printf("success");

来自 MSDN 文档: WSANO_数据 11004 有效名称,没有请求类型的数据记录。 请求的名称有效并且已在数据库中找到,但没有解析正确的关联数据。通常的示例是使用 DNS(域名服务器)的主机名到地址转换尝试(使用 gethostbyname 或 WSAAsyncGetHostByName)。返回 MX 记录,但没有 A 记录——表明主机本身存在,但无法直接访问。

有人可以建议为什么 locahost 会出现此错误吗?我无法弄清楚这里出了什么问题..

最佳答案

我遇到了同样的问题,尝试删除:

hints.ai_protocol= IPPROTO_TCP; 

它不会影响预期的结果,因为:

hints.ai_socktype= SOCK_STREAM;

告诉它只选择tcp连接。 也许你也可以只使用第一个,但我没有检查它。

关于c++ - getaddrinfo() 返回错误 "The requested name is valid, but no data of the requested type was found.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19266257/

相关文章:

python web-socket 客户端此日志记录来自哪里?

C编程: Reading a file and storing in array of struct

c - 使用hiredis一次调用发送多条记录

node.js - 这会利用多个 CPU 内核吗?

c++ - 查找服务器计算机中物理 CPU 插槽的数量

c++ - 在 C++ 中传递模板化对象?

c++ - 允许在一个完整表达式中 move 两次

c++ - 在 C++11 中实现递归代理模式

C 比较 'x < 0' 其中 'x' 的类型是任意的,即可能是无符号的

tcp - 是否可以通过 TCP 发送数据报?