如何获取外网IP,一般操作系统不自带这样的命令,需要通过特殊的网页获取,通过打开网页,获取所有源代码,然后分割字符串,取出对应字符串处的本机外网IP地址即可。至于检测是否通外网,很多人首先想到的是检测ping命令,然后看结果,这个方法速度比较慢,我想的方法就是直接用tcp连接百度的IP地址的80端口,如果100毫秒之内可以连上说明通外网。
具体代码如下:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool IPLive(QString ip, int port);
QString GetHtml(QString url);
QString GetNetIP(QString webCode);
QString GetAttribution(QString webCode);
public slots:
void on_btnGetLocalIP_clicked();
void on_btnGetWebIP_clicked();
void on_btnGetAttribution_clicked();
void on_btnCheckWeb_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//判断IP地址及端口是否在线
bool MainWindow::IPLive(QString ip, int port)
{
QTcpSocket tcpClient;
tcpClient.abort();
tcpClient.connectToHost(ip, port);
//100毫秒没有连接上则判断不在线
return tcpClient.waitForConnected(100);
}
//获取网页所有源代码
QString 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 QString(responseData);
}
//获取本机公网IP地址
QString MainWindow::GetNetIP(QString webCode)
{
QString ip = "";
QString web = webCode.replace(" ", "");
web = web.replace("\r", "");
web = web.replace("\n", "");
QStringList list = web.split("
");
if(list.count() >= 3)
{
ip = list[3];
list = ip.split("=");
if(list.count() == 2)
{
ip = list[1];
}
else
{
ip = "";
}
}
return ip;
}
//获取外网的归属地
QString MainWindow::GetAttribution(QString webCode)
{
QString attribution = "";
QString web = webCode.replace(" ", "");
web = web.replace("\r", "");
web = web.replace("\n", "");
web = web.replace("\t", "");
QStringList list = web.split("");
if(list.count() > 0)
{
attribution = list[0];
list = attribution.split(""
);
attribution = list.last();
list = attribution.split("");
attribution = list[0];
}
return attribution;
}
void MainWindow::on_btnGetLocalIP_clicked()
{
ui->txtLocalIP->setText("");
QList<QHostAddress> list = QNetworkInterface::allAddresses();
foreach (QHostAddress address, list)
{
//我们使用IPv4地址
if(address.protocol() == QAbstractSocket::IPv4Protocol)
{
ui->txtLocalIP->setText(address.toString());
break;
}
}
}
void MainWindow::on_btnGetWebIP_clicked()
{
ui->txtWebIP->setText(GetNetIP(GetHtml("http://whois.pconline.com.cn/")));
}
void MainWindow::on_btnGetAttribution_clicked()
{
ui->txtAttribution->setText(GetAttribution(GetHtml("http://whois.pconline.com.cn/")));
}
void MainWindow::on_btnCheckWeb_clicked()
{
//能接通百度IP说明可以通外网
bool ok=IPLive("202.108.22.5", 80);
ui->txtCheckWeb->setText(ok?"true":"false");
}