QT 获取本机所有IP

/* QT 获取本机所有IP */ #include #include #include bool isLanIP(uint ip) { // 127.x.x.x给本机地址使用 // 224.x.x.x为多播地址段 // 255.255.255.255为通用的广播地址 // 10.x.x.x,172.16.x.x和192.168.x.x供本地网使用 return (ip>>24 == 10) || ((ip>>16 >= (172<<8|16)) && (ip>>16 <= (172<<8|31))) || (ip>>16 == (192<<8|168)) || (ip>>24 == 127) || (ip>>24 == 224) || (ip == 0xFFFFFFFF); } QString GetLocalWanIP() { //QString hostname = QHostInfo::localHostName(); // 获取本机主机名 //QHostInfo host = QHostInfo::fromName(hostname); //QList &iplist = host.addresses(); // 获取本机IP列表 //foreach (QHostAddress address, iplist) // ui.textBrowser->append(address.toString()); QList &iplist = QNetworkInterface::allAddresses(); // 获取本机IP列表 foreach (QHostAddress address, iplist) { if (!isLanIP(address.toIPv4Address())) return address.toString(); } return QString(); } void q16::on_pushButton_clicked() { QString hostname = QHostInfo::localHostName(); // 获取本机主机名 QHostInfo host = QHostInfo::fromName(hostname); QList &iplist = host.addresses(); // 获取本机IP列表 foreach (QHostAddress address, iplist) ui.textBrowser->append(address.toString()); ui.textBrowser->append("-------------"); iplist = QNetworkInterface::allAddresses(); // 获取本机IP列表 foreach (QHostAddress address, iplist) ui.textBrowser->append(address.toString()); QString ip = GetLocalWanIP(); ui.textBrowser->append(ip); }

你可能感兴趣的:(C/C++,QT)