I need to get the ip address of api.openweathermap.org.

This is what I tried:

const char *host = strtok(url_data_string,"/"); // an IP address or a hostname, like "www.google.com" by itself

    struct addrinfo hints = {0};

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    //hints.ai_protocol = 80;
    struct addrinfo *addr = NULL;
    int ret = getaddrinfo(host, NULL , &hints, &addr);
    printf("asdd %s \n", host);
    char *ip_string = malloc(sizeof(char) * 2000);
    struct sockaddr_in target;
    void *addrCount = &((struct sockaddr_in*)addr->ai_addr)->sin_addr;
    if (ret == 0)
    {

        inet_ntop(addr->ai_family, addrCount, ip_string , sizeof(ip_string));
        freeaddrinfo(addr);
        printf("%s\n",ip_string);
    }
    else {
        printf("Error\n");
    }

The output is a random string,like "��jk5" for example,it should be 82.196.7.246

0 Answers