MainWindow.h代码:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QObject> #include <QTcpSocket> #include <QTcpServer> #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void slot_listen(QHostAddress addr,quint16 tar_port); void slot_newConnect(); void on_btn_static_clicked(); void on_btn_dyc_clicked(); private: Ui::MainWindow *ui; QTcpServer *newTcpServer; }; #endif // MAINWINDOW_HMainWindow.c代码:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QProcess> #include <QDebug> #include <QNetworkInterface> #define port 8888 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); newTcpServer = new QTcpServer(this); slot_listen(QHostAddress("192.168.1.135"),port); connect(newTcpServer, &QTcpServer::newConnection, this, &MainWindow::slot_newConnect); QList<QNetworkInterface> QNetworkInterfaceList = QNetworkInterface::allInterfaces(); for(int i = 0;i < QNetworkInterfaceList.count();i++){ qDebug() << QNetworkInterfaceList.at(i).humanReadableName(); ui->cb_name->addItem(QNetworkInterfaceList.at(i).humanReadableName()); } ui->le_ip->setText("192.168.1.135"); ui->le_mask->setText("255.255.255.0"); ui->le_gw->setText("192.168.1.0"); ui->le_dns->setText("114.114.114.114"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slot_listen(QHostAddress addr, quint16 tar_port) { if(newTcpServer->isListening()){ newTcpServer->close(); if(!newTcpServer->listen(addr,tar_port)){ qDebug() << "init: listen serverip fail!!!"; return; } }else{ if(!newTcpServer->listen(addr,tar_port)){ qDebug() << "init: listen serverip fail!!!"; return; } } } void MainWindow::slot_newConnect() { QTcpSocket *newSocket = newTcpServer->nextPendingConnection(); qDebug() << "MainWindow/slot_newConnect new connect ip = " << newSocket->peerAddress().toString(); } void MainWindow::on_btn_static_clicked() { QProcess *process = new QProcess(); QString name = ui->cb_name->currentText(); QString ip = ui->le_ip->text(); QString netmask = ui->le_mask->text(); QString gw = ui->le_gw->text(); QString dns = ui->le_dns->text(); QString command = "netsh interface ipv4 set address name = " + name + " source = static address = " + ip + " mask = " + netmask + " gateway = " + gw; QString adddns = "netsh interface ip set dns " + name + " static " + dns; qDebug()<<"command = "<< command; qDebug()<<"adddns = "<< adddns; process->start(command); process->waitForFinished(); process->start(adddns); process->waitForFinished(); delete process; } void MainWindow::on_btn_dyc_clicked() { QString name = ui->cb_name->currentText(); QString command = "netsh interface ip set address name = "+ name + " source=dhcp"; QString adddns = "netsh interface ip set dns name = " + name + " source=dhcp"; QProcess *process = new QProcess(); process->start(command); process->waitForFinished(); process->start(adddns); process->waitForFinished(); delete process; }