列举主机所有IP地址

int get_host_ipstring(LPWSTR ip_string) {
    struct hostent * Host;
    char hostname[512];
    if (gethostname(hostname, 512) != 0)
    {
        return -1;
    }
    Host = gethostbyname(hostname);

    if (Host == NULL)
    {
        return -1;
    }
    if (Host->h_addrtype == AF_INET)
    {
        int i = 0;
        struct in_addr addr;

        while (Host->h_addr_list[i] != 0)
        {
            addr.s_addr = *(u_long *) Host->h_addr_list[i++];

            char* p;
            p = inet_ntoa(addr);
            if (p == NULL)
            {
                return -1;
            }

	    //DO SOMETHING
        }
    }
    else
    {
        return -1;
    }

}



 


   

你可能感兴趣的:(列举主机所有IP地址)