所有调用 getaddrinfo() 时均返回 11001

发布于 2024-10-13 05:57:08 字数 3996 浏览 5 评论 0原文

连接到网络上的设备时出现问题。每当我调用 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 网站上获取,但看起来一切正常。

Having an issue connecting to a device on my network. Whenever I call getaddrinfo() it returns 11001. I have checked this with numerous different IP's in the IP_ADDRESS string (Global Var). I've checked all the non-working numbers with nslookup, and most exist there.

getaddrinfo-returns-always-11001-host-not-found
seems to be asking a similar question, but there's no answer there.

At the moment, my code is not even trying to connect to the remote device, just trying to resolve an IP. Once that works I can move on to bigger and messier problems.

Implementation:

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;}

Most of this code has been taken lock and stock from the msdn website, but it all seems to look ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩起发的微风 2024-10-20 05:57:08

这是“找不到主机”的错误代码。

查看 WinSock2.h 并搜索 WSABASEERR+1001WSAHOST_NOT_FOUND

Microsoft 的 docs 告诉您哪些错误代码getaddrinfo 返回。

That's the error code for "host not found".

Look in WinSock2.h and search for WSABASEERR+1001 or WSAHOST_NOT_FOUND.

Microsoft's docs tells you which error codes getaddrinfo returns.

你如我软肋 2024-10-20 05:57:08

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

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

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

I just ran into this problem as well ... getaddrinfo and gethostbyname are both failing with a 11001 error, but ping/nslookup are working for the same host names.

Turns out I had used the symbol server earlier, and I had symbols downloaded for all the Win32 DLL's in the same directory as my executable. Removing all the .pdb directories fixed my problem.

My guess is that gethostbyname or getaddrinfo fail if you have symbols and are debugging the application.

梦里寻她 2024-10-20 05:57:08

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

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

gethostbyname() will always return WSAHOST_NOT_FOUND (0x11001) if your environment block is empty, or missing SystemRoot.

(At a guess, it is WSAStartup() that actually requires it, but fails silently.)

可可 2024-10-20 05:57:08

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中

libhttp
I use getaddrinfo it refer to this link.below ,diff you is 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 */

in my project ,program use success in this way,you could learn more from libhttp.in my program call ,use it ok except proxy or not suppot ipv6 .example call func such as :
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))

when sock err happend i use

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

i use it ok in linux and win10

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文