Linux 获取IP地址

Linux 系统了ioctl的接口

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
	{
        ifc.ifc_len = sizeof(buf);
        ifc.ifc_buf = (caddr_t)buf;
        //获取所有的接口参数
        if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
        {
            intrface = ifc.ifc_len/sizeof(struct ifreq);
            while (intrface-- > 0)
            {
                //获取指定接口的IP地址
                if (!(ioctl(fd, SIOCGIFADDR, buf[intrface].ifr_name)))
                {
                    ip=(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
                    printf("Interface:%s IP:%s\n", buf[intrface].ifr_name, ip);
                }
            }
       }

        close(fd);
    }

 

你可能感兴趣的:(工作笔记)