0

我需要获取 api.openweathermap.org 的 IP 地址。

这是我尝试过的:

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

输出是一个随机字符串,比如“��jk5”,应该是82.196.7.246

4

0 回答 0