【发布时间】:2014-12-09 10:58:11
【问题描述】:
我正在尝试仅使用 IP 连接服务器,但是当我使用 gethostbyaddr 函数时,我得到了 error 11004,我使用了这样的函数:
WSADATA wsaData;
DWORD dwError;
struct hostent *remoteHost;
char host_addr[] = "127.0.0.1"; //or any other IP
struct in_addr addr = { 0 };
addr.s_addr = inet_addr(host_addr);
if (addr.s_addr == INADDR_NONE) {
printf("The IPv4 address entered must be a legal address\n");
return 1;
} else
remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
if (remoteHost == NULL) {
dwError = WSAGetLastError();
if (dwError != 0)
printf("error: %d", dwError)
}
我收到此错误:11004 by WSAGetLastError 函数:
WSANO_DATA
11004
Valid name, no data record of requested type.
The requested name is valid and was found in the database, but it does not have the correct
associated data being resolved for. The usual example for this is a host name-to-address
translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS
(Domain Name Server). An MX record is returned but no A record—indicating the host itself
exists, but is not directly reachable.
PS:当我使用gethostbyname 时,我的代码可以正常工作。
【问题讨论】:
-
在 POSIX 中确实如此,但对于 winsock 则不是这样,请查看 Microsoft 文档:msdn.microsoft.com/en-us/library/windows/desktop/…
-
天啊,我忘了温索克的恐怖。评论已删除。谢谢。
-
你为什么要使用
gethostbyaddr()?您已经有了 IP 地址,这就是您将connect()发送到服务器所需的全部内容。