getaddrinfo() 返回错误"The requested name is valid, but no data of the requested type was found."
getaddrinfo() returns error "The requested name is valid, but no data of the requested type was found."
我在我的示例程序中使用 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 - %sn", gai_rc, gai_strerrorA(gai_rc));
return false;
}
printf("success");
从 MSDN 文档:WSANO_DATA11004名称有效,没有请求类型的数据记录。请求的名称有效,并且在数据库中找到,但它没有解析正确的关联数据。通常的例子是主机名到地址的转换尝试(使用gethostbyname或WSAAsyncGetHostByName(,它使用DNS(域名服务器(。返回 MX 记录,但不返回 A 记录 - 指示主机本身存在,但无法直接访问。
有人可以建议为什么它为locahost提供此错误。我无法弄清楚这里的问题是什么。
我遇到了同样的问题,请尝试删除:
hints.ai_protocol= IPPROTO_TCP;
它不会影响预期的结果,因为:
hints.ai_socktype= SOCK_STREAM;
告诉它只选择 TCP 连接。也许你也可以只使用第一个,但我没有检查它。
相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 收到错误"invalid use of non-static data member 'stu::n' "
- C++ 初始化 .data 部分中的变量
- 模式"allocate memory or use existing data"
- boost::asio data owning `ConstBufferSequence`
- Python to C++ Data structure API
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- protobuf C++ SQLite handle blob data
- 错误:字段'dateOfBirth'的类型不完整'Poco::Data::Date'
- 为什么构造函数 Message(const T&data) 与 Message(T&& data) 冲突,当 T = int&时?
- "thread-safe data"与"thread-safe code/functions"的区别
- 如何找到目标文件 *.o 的 ram rom 使用情况(.bss .text .rodata .data)?
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- 使用 .data() 将字符数组转换为 std::string 不会转换整个数组
- *(int*)&data[18]在这段代码中实际上做了什么?
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 构造中错误:未在此范围中声明"data"
- getaddrinfo() 返回错误"The requested name is valid, but no data of the requested type was found."