firemail
标题:
QT 获取本地IP MAC地址 外网IP 位置信息 连网状态 操作系统信息
[打印本页]
作者:
Qter
时间:
2018-4-10 15:21
标题:
QT 获取本地IP MAC地址 外网IP 位置信息 连网状态 操作系统信息
本帖最后由 Qter 于 2018-4-11 09:39 编辑
在pro文件加了一行
QT += network
#include <QHostAddress>
#include <QNetworkInterface>
QString MainWindow::gethostIp()
{
QString strIpAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// 获取第一个本主机的IPv4地址
int nListSize = ipAddressesList.size();
for (int i = 0; i < nListSize; ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address())
{
strIpAddress = ipAddressesList.at(i).toString();
break;
}
}
// 如果没有找到,则以本地IP地址为IP
if (strIpAddress.isEmpty())
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
return strIpAddress;
}
QString MainWindow::gethostMac()
{
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
int nCnt = nets.count();
QString strMacAddr = "";
for(int i = 0; i < nCnt; i ++)
{
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning)
&& !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
{
strMacAddr = nets[i].hardwareAddress();
break;
}
}
return strMacAddr;
}
复制代码
连网状态
bool MainWindow::IPLive(QString ip, int port)
{
QTcpSocket tcpClient;
tcpClient.abort();
tcpClient.connectToHost(ip, port);
//100毫秒没有连接上则判断不在线
return tcpClient.waitForConnected(100);
}
调用 判断百度 80端口是否连通
qDebug() << IPLive("202.108.22.5", 80);
复制代码
外网IP及位置信息 通过网页
http://whois.pconline.com.cn/
查询解析
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTextCodec>
QByteArray MainWindow::GetHtml(QString url)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
QByteArray responseData;
QEventLoop eventLoop;
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
eventLoop.exec();
responseData = reply->readAll();
return responseData;
}
QString MainWindow::GetNetIPPconline(QByteArray responseData)
{
QTextCodec* pTextCodec = QTextCodec::codecForName("GB2312");
if (NULL == pTextCodec)
{
return "";
}
QString webCode = pTextCodec->toUnicode(responseData);
QString ipinfo = "";
QString web = webCode.replace(" ", "");
web = web.replace("\r", "");
web = web.replace("\n", "");
web = web.replace("\t", "");
QStringList list = web.split("</form>");
if(list.count() > 0)
{
ipinfo = list[0];
list = ipinfo.split("value=");
ipinfo = list.last();
list = ipinfo.split("><");
ipinfo = list[0];
}
return ipinfo.replace("\"","");
}
//获取外网的归属地
QString MainWindow::GetAttributionPconline(QByteArray responseData)
{
QTextCodec* pTextCodec = QTextCodec::codecForName("GB2312");
if (NULL == pTextCodec)
{
return "";
}
QString webCode = pTextCodec->toUnicode(responseData);
// webCode = webCode.fromLocal8Bit(responseData.data());
QString attribution = "";
QString web = webCode; //.replace(" ", "");
web = web.replace("\r", "");
web = web.replace("\n", "");
web = web.replace("\t", "");
QStringList list = web.split("</form>");
if(list.count() > 0)
{
attribution = list[0];
list = attribution.split("<p>");
attribution = list.last();
list = attribution.split("</p>");
attribution = list[0];
}
QStringList addInfo = attribution.split(":");
if(addInfo.count() > 1){
return addInfo[1].trimmed();
}
return attribution;
}
调用
qDebug() << GetNetIPPconline(GetHtml("http://whois.pconline.com.cn/"));
qDebug() << GetAttributionPconline(GetHtml("http://whois.pconline.com.cn/"));
复制代码
只查询外网ip 通过
http://www.3322.org/dyndns/getip
//获取本机公网IP地址
QString MainWindow::GetNetIP3322(QString webCode)
{
QString ip = "";
QString web = webCode.replace(" ", "");
web = web.replace("\r", "");
ip = web.replace("\n", "");
return ip;
}
qDebug() << GetNetIP3322(GetHtml("http://www.3322.org/dyndns/getip"));
复制代码
获取操作系统信息
#include <QSysInfo>
QString MainWindow::getOSInfo()
{
QString osInfo;
qDebug() << "buildCpuArchitecture: " << QSysInfo::buildCpuArchitecture();
osInfo = QSysInfo::prettyProductName() +" [" +QSysInfo::productType() +" "+ QSysInfo::productVersion() +"(" + QString::number(QSysInfo::windowsVersion()) + ")]";
osInfo += " | " + QSysInfo::buildAbi() + " | " + QSysInfo::currentCpuArchitecture() + " | " + QSysInfo::kernelType() + " " + QSysInfo::kernelVersion() + " | " + QSysInfo::machineHostName();
return osInfo;
}
复制代码
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3