【问题标题】:gethostbyaddr return NULL even with a valid IP即使使用有效的 IP,gethostbyaddr 也会返回 NULL
【发布时间】: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() 发送到服务器所需的全部内容。

标签: c sockets winapi winsock2


【解决方案1】:

你忘了初始化winsock

#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib, "Ws2_32.lib")

int main() {
   WSADATA wsaData;
   // Initialize Winsock
   int wret = WSAStartup(MAKEWORD(2,2), &wsaData);
   if (wret != 0) {
      printf("WSAStartup failed: %d\n", wret);
      return 1;
   }

   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);
   }
   else {
      printf("Hostname: %s\n", remoteHost->h_name);
   }

   WSACleanup();

   return 0;
}

上面的代码适用于我的 127.0.0.1 和我 LAN 上的一些远程主机。但有趣的是,对于可能没有主机名的路由器,我得到了相同的 11004 winsock 错误。

在那种情况下。

struct hostent* hostbyname = gethostbyname(host_addr);

if (hostbyname == NULL) {
   dwError = WSAGetLastError();
   if (dwError != 0)
      printf("error: %d", dwError);
}
else {
   printf("Hostname: %s\n", hostbyname->h_name);
}

也不返回主机名,并导致相同的错误 - 11004。

对你的经历感到很困惑。

【讨论】:

  • 好吧,实际上我只放了一段代码,因为我认为它是隐式的,我的错误,但是我初始化了winsock,并且使用gethostbyname 可以正常工作。
  • @Alex gethostbyaddr 对我来说很好用。你的操作系统是什么?
  • 我尝试了我的路由器,我也收到了这个错误,我使用的是 windows 8
  • @Alex - 也许它是 Windows 8 特定的。我在 Windows 7 上。
猜你喜欢
  • 2018-05-29
  • 2015-12-30
  • 2018-02-12
  • 2021-06-24
  • 1970-01-01
  • 2023-03-22
  • 2020-11-29
  • 2017-03-20
  • 1970-01-01
相关资源
最近更新 更多