Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getaddrinfo() function throw error n 11003

This drag me mad

#undef UNICODE

#include "stdafx.h"
#include <WS2tcpip.h>
#include <WinSock2.h>
#include <string>
#pragma comment(lib,"Ws2_32.lib")

using namespace std;

int _tmain(int argc, char* argv[])
{

    WSADATA wsa;
    int error = WSAStartup(MAKEWORD(2,2),&wsa);
    if (error != 0)
    {
        printf("An error in startup %d\n",WSAGetLastError());
        system("pause");
    }

    addrinfo hints,
        * result = NULL,
        * ptr = NULL;
    hints.ai_family = AF_UNSPEC;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_socktype = SOCK_STREAM;

    error = getaddrinfo(argv[1],NULL,&hints,&result);
    if (error != 0)
    {
        printf("An error in getaddrinfo %d\n",WSAGetLastError());
        system("pause");
    }

    char stringbuffer[2075];
    int len = sizeof(stringbuffer);
    for(ptr = result; ptr->ai_next != NULL; ptr = ptr->ai_next)
    {
        if(ptr->ai_family == AF_INET)
        {
            printf("Address: %s\n",InetNtop(ptr->ai_family,ptr->ai_addr,stringbuffer,len));
        }
    }

    return 0;
}

getaddrinfo throw 11003 error, i've compared a lot of internet source with main but i can't figure out why getaddrinfo fail! i've read the winsock error code description and it says that 11003 "indicates that some sort of nonrecoverable error occurred during a database lookup" so thanks in advance ! EDIT: i've asked somewhere else but none give me the solution,i'm blocked on this and i can't move forward

like image 799
SirLeoncavallo Avatar asked Oct 16 '25 08:10

SirLeoncavallo


1 Answers

As nos has mentioned in comments

memset(&hints, 0, sizeof(hints))

should do the trick.

getaddrinfo() tries to fill up with existing data provided. Garbage data is confusing the function.

like image 114
Antarus Avatar answered Oct 17 '25 21:10

Antarus