【问题标题】:getaddrinfo() returns error "The requested name is valid, but no data of the requested type was found."getaddrinfo() 返回错误“请求的名称有效,但未找到请求类型的数据。”
【发布时间】:2013-10-09 08:00:58
【问题描述】:

我在我的示例程序中使用 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 文档: WSANA_DATA 11004 有效名称,没有请求类型的数据记录。 请求的名称有效并且在数据库中找到,但它没有正确的关联数据正在解析。通常的示例是使用 DNS(域名服务器)的主机名到地址转换尝试(使用 gethostbyname 或 WSAAsyncGetHostByName)。返回一条 MX 记录,但没有 A 记录——表明主机本身存在,但不可直接访问。

有人可以建议为什么它会为 locahost 提供此错误。我无法弄清楚这里有什么问题..

【问题讨论】:

  • 使用 perror 代替 printf
  • @UmerFarooq getaddrinfo 函数未设置 errno。它有自己的错误代码。
  • 我认为错误信息非常具有描述性。
  • @JoachimPileborg 感谢您的告知 :)
  • 为什么将端口放在请求中?您是否希望该服务使用端口 3333?您在 /etc/services 中是否有用于端口 3333 的内容?如果您只提供主机参数和提示,它是否有效?

标签: c++ c sockets


【解决方案1】:

我有同样的问题,尝试删除:

hints.ai_protocol= IPPROTO_TCP; 

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

hints.ai_socktype= SOCK_STREAM;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多