所有对getaddrinfo()的调用都返回11001

时间:2011-01-24 03:49:40

标签: c++ c windows sockets winsock2

连接到网络上的设备时出现问题。每当我调用getaddrinfo()时它返回11001.我已经在IP_ADDRESS字符串(Global Var)中检查了许多不同的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网站上获取锁定和库存的,但一切看起来都不错。

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

我刚刚遇到这个问题...... getaddrinfogethostbyname都失败了11001错误,但是ping / nslookup正在使用相同的主机名。

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

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

答案 2 :(得分:0)

如果您的环境块为空或缺少gethostbyname()

WSAHOST_NOT_FOUND将始终返回SystemRoot(0x11001)。

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

答案 3 :(得分:0)

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。 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中正常使用