Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ getaddrinfo()返回错误";请求的名称有效,但未找到请求类型的数据;_C++_C_Sockets - Fatal编程技术网

C++ getaddrinfo()返回错误";请求的名称有效,但未找到请求类型的数据;

C++ getaddrinfo()返回错误";请求的名称有效,但未找到请求类型的数据;,c++,c,sockets,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)

我正在示例程序中使用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记录,但没有指示主机本身存在但无法直接访问的记录


有人能告诉我为什么locahost会出现这个错误吗。我无法找出此处的问题所在。

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

hints.ai_protocol= IPPROTO_TCP; 
这不会影响预期结果,因为:

hints.ai_socktype= SOCK_STREAM;
正在告诉它仅选择tcp连接。
也许您也可以只使用第一个,但我没有检查。

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