现在的位置: 首页 > 综合 > 正文

获取主机的ip

2017年09月01日 ⁄ 综合 ⁄ 共 390字 ⁄ 字号 评论关闭

int get_ip_num()
{
    struct hostent * Host;
    char hostname[512];
    int ip_num = 0;
    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)
        {
            i++;
            ip_num++;
        }
    }

    return ip_num;
}

抱歉!评论已关闭.