【问题标题】:11001 returned on all calls to getaddrinfo()对 getaddrinfo() 的所有调用均返回 11001
【发布时间】:2011-06-14 06:54:27
【问题描述】:

在连接到我的网络上的设备时遇到问题。每当我调用 getaddrinfo() 时,它都会返回 11001。我用 IP_ADDRESS 字符串(全局变量)中的许多不同 IP 对此进行了检查。我用 nslookup 检查了所有无效的数字,大部分都存在。

getaddrinfo-returns-always-11001-host-not-found 似乎在问类似的问题,但那里没有答案。

目前,我的代码甚至没有尝试连接到远程设备,只是尝试解析 IP。一旦成功,我就可以继续解决更大更混乱的问题。

实施:

int connectToDevice(char *sendbuf, char *recvbuf, SOCKET ConnectSocket)
{
WSADATA wsaData;
    struct addrinfo *result = NULL,
                *ptr = NULL,
                hints;
struct timeval tval;

fd_set rset, wset;


int iResult;
u_long mode = -1;

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

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

//Resolve the server address and port
iResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) 
{
    printf("getaddrinfo failed with error: %d\n", iResult);
    WSACleanup();
    return 1;
}



// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) 
{

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) 
    {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //set socket to non-blocking
    iResult = ioctlsocket(ConnectSocket, FIONBIO, &mode); //if mode is set to non-zero, socket set to non-blocking.
    if(iResult != NO_ERROR)
    {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }


    // Connect to server.
    iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR  ) //if an error and not WSAEWOULDBLOCK, then close socket and try next address
    {
        if(WSAEWOULDBLOCK != WSAGetLastError())
        {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;                           //this returns control to the For loop. I.e. if a socket error, try next address
        }
        else    //otherwise if the error was WSAEWOULDBLOCK, then use select to check for connections.
        {
            FD_ZERO(&rset); //initialise fd_sets for reading and writing; both the same.
            FD_SET(ConnectSocket, &rset);
            wset = rset;

            //set tval to timeout value
            tval.tv_sec = TIMEOUT;
            tval.tv_usec= 0;

            //select statement
            //select ignores first parameter
            //select takes 3xfd_sets, read set, write set, and exception set.
            //select's last parameter is timeout in the form of a timeval struct
            //if return == 0, timeout occured.
            //if return == SOCKET_ERROR, error occured, use WSAGetLastError to check for details.

            iResult = select(ConnectSocket, &rset, &wset, NULL, &tval);
            if (iResult ==0)
            {
                closesocket(ConnectSocket);
                printf("Timeout reached, closing socket");
                WSACleanup();
                return 1;
            }
            else if(iResult == SOCKET_ERROR)
            {
                printf("socket failed with error: %ld\n", WSAGetLastError());
                WSACleanup();
                return 1;
            }

        }

    }

    break;  //Breaks out of the for loop. Will only occur if continue not executed
}

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET)
{
    printf("Unable to connect to server!\n");
    WSACleanup();
    return 1;
}

return 0;}

大部分代码已从 msdn 网站上锁定和库存,但看起来一切正常。

【问题讨论】:

  • IP_ADDRESS 已被各种设置为:服务器(本地服务器名称)192.168.1.1(服务器 IP)127.0.0.1 192.168.1.66(设备 IP)192.168.1.120(我的电脑)
  • 如果你给它一个主机名,它是否可以工作,比如localhost
  • @romkyns,不,它仍然返回 11001,我已经使用本地服务器名称和 localhost 对其进行了测试
  • 我遇到了完全相同的问题。对此有何解决方案?
  • 您找到解决方案了吗?

标签: c++ c windows sockets winsock2


【解决方案1】:

这是找不到主机的错误代码。查看 WinSock2.h。搜索WSABASEERR+1001WSAHOST_NOT_FOUND

Microsoft 会告诉您here getaddrinfo 返回的错误代码。

【讨论】:

  • 系统编程 101:阅读文档并花一点时间明确处理和报告所有允许的返回码。
  • MS 的垃圾文件缺少线程安全的 gai_strerror。最好使用 WSAGetLastError 和 FormatMessage。或者滚动你自己的,我已经做了几次,因为在 Winsock[2] 的无尽坑中,在某些情况下 FormatMessage 提供的内容在上下文中没有意义。如果我很快想到一个例子,我会更新......
  • 嗨,伙计们,我的代码正在处理收到的错误,我知道错误的含义,但它在地址已知的情况下突然出现,并通过 nslookup 或包含的数字字符串解决IP 地址被传入,并且该地址存在于网络中。 编辑我会试试那个 WSAGetLastError 包装器,然后告诉你它是怎么回事!
  • 大家好,我尝试使用 WSAGetLastError() 来查看在调用 getaddrinfo 时是否存在未检测到的错误,但它只返回 0。据我所知,getaddrinfo 调用无法无论格式如何,都解析地址。关于为什么会发生这种情况的任何想法?
  • \System32\Drivers\etc\hosts 中有什么?
【解决方案2】:

我也遇到了这个问题...getaddrinfogethostbyname 都因 11001 错误而失败,但 ping/nslookup 对相同的主机名有效。

原来我之前使用过符号服务器,并且我为所有 Win32 DLL 下载了符号,这些符号与我的可执行文件位于同一目录中。删除所有 .pdb 目录解决了我的问题。

我的猜测是,如果您有符号并且正在调试应用程序,gethostbynamegetaddrinfo 会失败。

【讨论】:

    【解决方案3】:

    如果您的环境块为空或缺少SystemRootgethostbyname() 将始终返回WSAHOST_NOT_FOUND (0x11001)。

    (猜测是 WSAStartup() 实际需要它,但默默地失败了。)

    【讨论】:

      【解决方案4】:

      libhttp 我用getaddrinfo它指的是这个链接。下面,你是hints.ai_family = af;

        int  XX_httplib_inet_pton(int af, const char *src, void *dst, size_t dstlen)
      {
          struct addrinfo hints;
          struct addrinfo *res;
          struct addrinfo *ressave;
          int func_ret;
          int gai_ret;
      
          func_ret = 0;
      
          memset(&hints, 0, sizeof(struct addrinfo));
          hints.ai_family = af;
      
          gai_ret = getaddrinfo(src, NULL, &hints, &res);
      
          if (gai_ret != 0) {
      
          /*
          * gai_strerror could be used to convert gai_ret to a string
          * POSIX return values: see
          * http://pubs.opengroup.org/onlinepubs/9699919799/functions/freeaddrinfo.html
          *
          * Windows return values: see
          * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520%28v=vs.85%29.aspx
          */
           odprintf("af[%d] getaddrinfo ret[%d] [%d]\n",af,gai_ret,WSAGetLastError());
          return 0;
          }
      
          ressave = res;
      
          while (res) {
      
          if (dstlen >= res->ai_addrlen) {
      
              memcpy(dst, res->ai_addr, res->ai_addrlen);
              func_ret = 1;
          }
          res = res->ai_next;
          }
      
          freeaddrinfo(ressave);
          return func_rett;
      }  /* XX_httplib_inet_pton */
      

      在我的项目中,程序以这种方式使用成功,您可以从 libhttp 中了解更多信息。在我的程序调用中,除了代理或不支持 ipv6 之外,都可以使用它。例如调用 func 例如: XX_httplib_inet_pton(AF_INET, "127.0.0.1", &sa->sin, sizeof(sa->sin))

      XX_httplib_inet_pton(AF_INET, host, &sa->sin, sizeof(sa->sin))
      

      XX_httplib_inet_pton(AF_INET6, "fe80::f816:3eff:fe49:50c6%6", &sa->sin6, sizeof(sa->sin6))

      XX_httplib_inet_pton(AF_INET, host, &sa->sin, sizeof(sa->sin))
      

      XX_httplib_inet_pton(AF_INET6, "::1", &sa->sin6, sizeof(sa->sin6))

      XX_httplib_inet_pton(AF_INET, host, &sa->sin, sizeof(sa->sin))
      

      当袜子出错时我使用

         int eno=  WSAGetLastError();
       char erbuf[40]; 
        FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ARGUMENT_ARRAY,
        NULL,eno,0,erbuf,sizeof(erbuf),NULL);OutputDebugStringA(erbuf);
      

      我在linux和win10上都可以用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-16
        • 1970-01-01
        • 2018-02-12
        • 2021-10-05
        • 1970-01-01
        • 2023-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多