更新网络调试助手
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
FORMS += \
|
||||
FORMS += \
|
||||
$$PWD/frmmain.ui \
|
||||
$$PWD/frmtcpclient.ui \
|
||||
$$PWD/frmtcpserver.ui \
|
||||
$$PWD/frmudpclient.ui \
|
||||
$$PWD/frmudpserver.ui
|
||||
|
||||
HEADERS += \
|
||||
HEADERS += \
|
||||
$$PWD/frmmain.h \
|
||||
$$PWD/frmtcpclient.h \
|
||||
$$PWD/frmtcpserver.h \
|
||||
$$PWD/frmudpclient.h \
|
||||
$$PWD/frmudpserver.h
|
||||
|
||||
SOURCES += \
|
||||
SOURCES += \
|
||||
$$PWD/frmmain.cpp \
|
||||
$$PWD/frmtcpclient.cpp \
|
||||
$$PWD/frmtcpserver.cpp \
|
||||
$$PWD/frmudpclient.cpp \
|
||||
$$PWD/frmudpserver.cpp
|
||||
|
||||
@@ -47,6 +47,11 @@
|
||||
<string>TCP服务器</string>
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="frmUdpClient" name="tabUdpClient">
|
||||
<attribute name="title">
|
||||
<string>UDP客户端</string>
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="frmUdpServer" name="tabUdpServer">
|
||||
<attribute name="title">
|
||||
<string>UDP服务器</string>
|
||||
@@ -75,6 +80,12 @@
|
||||
<header>frmudpserver.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>frmUdpClient</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>frmudpclient.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
||||
@@ -6,7 +6,7 @@ frmTcpClient::frmTcpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
frmTcpClient::~frmTcpClient()
|
||||
@@ -56,8 +56,7 @@ void frmTcpClient::initConfig()
|
||||
ui->txtServerPort->setText(QString::number(App::TcpServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
timer->setInterval(App::IntervalTcpClient);
|
||||
App::AutoSendTcpClient ? timer->start() : timer->stop();
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmTcpClient::saveConfig()
|
||||
@@ -72,8 +71,21 @@ void frmTcpClient::saveConfig()
|
||||
App::TcpServerPort = ui->txtServerPort->text().trimmed().toInt();
|
||||
App::writeConfig();
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmTcpClient::changeTimer()
|
||||
{
|
||||
timer->setInterval(App::IntervalTcpClient);
|
||||
App::AutoSendTcpClient ? timer->start() : timer->stop();
|
||||
if (App::AutoSendTcpClient) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpClient::append(int type, const QString &data, bool clear)
|
||||
|
||||
@@ -27,6 +27,7 @@ private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void changeTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
void connected();
|
||||
|
||||
@@ -102,7 +102,7 @@
|
||||
<item>
|
||||
<widget class="QLabel" name="labServerIP">
|
||||
<property name="text">
|
||||
<string>服务器IP地址</string>
|
||||
<string>服务器地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
@@ -6,6 +6,7 @@ frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initIP();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
@@ -31,6 +32,38 @@ void frmTcpServer::initForm()
|
||||
QUIHelper::setLabStyle(ui->labCount, 3);
|
||||
}
|
||||
|
||||
void frmTcpServer::initIP()
|
||||
{
|
||||
//获取本机所有IP
|
||||
QStringList ips;
|
||||
QList<QNetworkInterface> netInterfaces = QNetworkInterface::allInterfaces();
|
||||
foreach(const QNetworkInterface &netInterface, netInterfaces) {
|
||||
//移除虚拟机和抓包工具的虚拟网卡
|
||||
QString humanReadableName = netInterface.humanReadableName().toLower();
|
||||
if(humanReadableName.startsWith("vmware network adapter") || humanReadableName.startsWith("npcap loopback adapter")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//过滤当前网络接口
|
||||
bool flag = (netInterface.flags() == (QNetworkInterface::IsUp | QNetworkInterface::IsRunning | QNetworkInterface::CanBroadcast | QNetworkInterface::CanMulticast));
|
||||
if(flag) {
|
||||
QList<QNetworkAddressEntry> addrs = netInterface.addressEntries();
|
||||
foreach(QNetworkAddressEntry addr, addrs) {
|
||||
//只取出IPV4的地址
|
||||
if(addr.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
||||
QString ip4 = addr.ip().toString();
|
||||
if(ip4 != "127.0.0.1") {
|
||||
ips.append(ip4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ui->cboxListenIP->addItems(ips);
|
||||
ui->cboxListenIP->addItem("127.0.0.1");
|
||||
}
|
||||
|
||||
void frmTcpServer::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(App::HexSendTcpServer);
|
||||
@@ -46,19 +79,21 @@ void frmTcpServer::initConfig()
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(App::AutoSendTcpServer);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckSelectAll->setChecked(App::SelectAllTcpServer);
|
||||
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalTcpServer)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(App::TcpListenIP));
|
||||
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort->setText(QString::number(App::TcpListenPort));
|
||||
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
timer->setInterval(App::IntervalTcpServer);
|
||||
App::AutoSendTcpServer ? timer->start() : timer->stop();
|
||||
ui->ckSelectAll->setChecked(App::SelectAllTcpServer);
|
||||
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmTcpServer::saveConfig()
|
||||
@@ -67,14 +102,28 @@ void frmTcpServer::saveConfig()
|
||||
App::HexReceiveTcpServer = ui->ckHexReceive->isChecked();
|
||||
App::AsciiTcpServer = ui->ckAscii->isChecked();
|
||||
App::DebugTcpServer = ui->ckDebug->isChecked();
|
||||
App::AutoSendTcpServer = ui->ckAutoSend->isChecked();
|
||||
App::SelectAllTcpServer = ui->ckSelectAll->isChecked();
|
||||
App::AutoSendTcpServer = ui->ckAutoSend->isChecked();
|
||||
App::IntervalTcpServer = ui->cboxInterval->currentText().toInt();
|
||||
App::TcpListenIP = ui->cboxListenIP->currentText();
|
||||
App::TcpListenPort = ui->txtListenPort->text().trimmed().toInt();
|
||||
App::SelectAllTcpServer = ui->ckSelectAll->isChecked();
|
||||
App::writeConfig();
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmTcpServer::changeTimer()
|
||||
{
|
||||
timer->setInterval(App::IntervalTcpServer);
|
||||
App::AutoSendTcpServer ? timer->start() : timer->stop();
|
||||
if (App::AutoSendTcpServer) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpServer::append(int type, const QString &data, bool clear)
|
||||
@@ -117,25 +166,11 @@ void frmTcpServer::append(int type, const QString &data, bool clear)
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmTcpServer::sendData(const QString &data)
|
||||
{
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
tcpServer->writeData(data);
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
tcpServer->writeData(list.at(0), list.at(1).toInt(), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpServer::clientConnected(const QString &ip, int port)
|
||||
{
|
||||
QString str = QString("%1:%2").arg(ip).arg(port);
|
||||
ui->listWidget->addItem(str);
|
||||
ui->labCount->setText(QString("共 %1 个连接").arg(ui->listWidget->count()));
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmTcpServer::clientDisconnected(const QString &ip, int port)
|
||||
@@ -150,7 +185,7 @@ void frmTcpServer::clientDisconnected(const QString &ip, int port)
|
||||
}
|
||||
|
||||
delete ui->listWidget->takeItem(row);
|
||||
ui->labCount->setText(QString("共 %1 个连接").arg(ui->listWidget->count()));
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmTcpServer::sendData(const QString &ip, int port, const QString &data)
|
||||
@@ -214,7 +249,16 @@ void frmTcpServer::on_btnSend_clicked()
|
||||
return;
|
||||
}
|
||||
|
||||
sendData(data);
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
tcpServer->writeData(data);
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
tcpServer->writeData(list.at(0), list.at(1).toInt(), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpServer::on_btnClose_clicked()
|
||||
|
||||
@@ -25,13 +25,12 @@ private:
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initIP();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void changeTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void sendData(const QString &data);
|
||||
|
||||
void clientConnected(const QString &ip, int port);
|
||||
void clientDisconnected(const QString &ip, int port);
|
||||
void sendData(const QString &ip, int port, const QString &data);
|
||||
|
||||
@@ -99,6 +99,16 @@
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxInterval"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labListenIP">
|
||||
<property name="text">
|
||||
<string>监听地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxListenIP"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labListenPort">
|
||||
<property name="text">
|
||||
@@ -152,7 +162,7 @@
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>共 0 个连接</string>
|
||||
<string>共 0 个客户端</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
@@ -165,7 +175,7 @@
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckSelectAll">
|
||||
<property name="text">
|
||||
<string>对所有已连接客户端</string>
|
||||
<string>对所有客户端发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@@ -224,6 +234,26 @@
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>ckHexReceive</tabstop>
|
||||
<tabstop>ckHexSend</tabstop>
|
||||
<tabstop>ckAscii</tabstop>
|
||||
<tabstop>ckShow</tabstop>
|
||||
<tabstop>ckDebug</tabstop>
|
||||
<tabstop>ckAutoSend</tabstop>
|
||||
<tabstop>cboxInterval</tabstop>
|
||||
<tabstop>cboxListenIP</tabstop>
|
||||
<tabstop>txtListenPort</tabstop>
|
||||
<tabstop>btnListen</tabstop>
|
||||
<tabstop>btnSave</tabstop>
|
||||
<tabstop>btnClear</tabstop>
|
||||
<tabstop>btnClose</tabstop>
|
||||
<tabstop>listWidget</tabstop>
|
||||
<tabstop>ckSelectAll</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
||||
214
nettool/form/frmudpclient.cpp
Normal file
214
nettool/form/frmudpclient.cpp
Normal file
@@ -0,0 +1,214 @@
|
||||
#include "frmudpclient.h"
|
||||
#include "ui_frmudpclient.h"
|
||||
#include "quiwidget.h"
|
||||
|
||||
frmUdpClient::frmUdpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdpClient)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
frmUdpClient::~frmUdpClient()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmUdpClient::initForm()
|
||||
{
|
||||
udpSocket = new QUdpSocket(this);
|
||||
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
ui->cboxInterval->addItems(App::Intervals);
|
||||
ui->cboxData->addItems(App::Datas);
|
||||
}
|
||||
|
||||
void frmUdpClient::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(App::HexSendUdpClient);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(App::HexReceiveUdpClient);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(App::AsciiUdpClient);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(App::DebugUdpClient);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(App::AutoSendUdpClient);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalUdpClient)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerIP->setText(App::UdpServerIP);
|
||||
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerPort->setText(QString::number(App::UdpServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmUdpClient::saveConfig()
|
||||
{
|
||||
App::HexSendUdpClient = ui->ckHexSend->isChecked();
|
||||
App::HexReceiveUdpClient = ui->ckHexReceive->isChecked();
|
||||
App::AsciiUdpClient = ui->ckAscii->isChecked();
|
||||
App::DebugUdpClient = ui->ckDebug->isChecked();
|
||||
App::AutoSendUdpClient = ui->ckAutoSend->isChecked();
|
||||
App::IntervalUdpClient = ui->cboxInterval->currentText().toInt();
|
||||
App::UdpServerIP = ui->txtServerIP->text().trimmed();
|
||||
App::UdpServerPort = ui->txtServerPort->text().trimmed().toInt();
|
||||
App::writeConfig();
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmUdpClient::changeTimer()
|
||||
{
|
||||
timer->setInterval(App::IntervalUdpClient);
|
||||
if (App::AutoSendUdpClient) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpClient::append(int type, const QString &data, bool clear)
|
||||
{
|
||||
static int currentCount = 0;
|
||||
static int maxCount = 100;
|
||||
|
||||
if (clear) {
|
||||
ui->txtMain->clear();
|
||||
currentCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentCount >= maxCount) {
|
||||
ui->txtMain->clear();
|
||||
currentCount = 0;
|
||||
}
|
||||
|
||||
if (ui->ckShow->isChecked()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//过滤回车换行符
|
||||
QString strData = data;
|
||||
strData = strData.replace("\r", "");
|
||||
strData = strData.replace("\n", "");
|
||||
|
||||
//不同类型不同颜色显示
|
||||
QString strType;
|
||||
if (type == 0) {
|
||||
strType = "发送";
|
||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
||||
} else {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("red"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmUdpClient::readData()
|
||||
{
|
||||
QHostAddress host;
|
||||
quint16 port;
|
||||
QByteArray data;
|
||||
QString buffer;
|
||||
|
||||
while (udpSocket->hasPendingDatagrams()) {
|
||||
data.resize(udpSocket->pendingDatagramSize());
|
||||
udpSocket->readDatagram(data.data(), data.size(), &host, &port);
|
||||
|
||||
if (App::HexReceiveUdpClient) {
|
||||
buffer = QUIHelper::byteArrayToHexStr(data);
|
||||
} else if (App::AsciiUdpClient) {
|
||||
buffer = QUIHelper::byteArrayToAsciiStr(data);
|
||||
} else {
|
||||
buffer = QString(data);
|
||||
}
|
||||
|
||||
QString ip = host.toString();
|
||||
ip = ip.replace("::ffff:", "");
|
||||
if (ip.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer);
|
||||
append(1, str);
|
||||
|
||||
if (App::DebugUdpClient) {
|
||||
int count = App::Keys.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (App::Keys.at(i) == buffer) {
|
||||
sendData(ip, port, App::Values.at(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpClient::sendData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (App::HexSendUdpClient) {
|
||||
buffer = QUIHelper::hexStrToByteArray(data);
|
||||
} else if (App::AsciiUdpClient) {
|
||||
buffer = QUIHelper::asciiStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toLatin1();
|
||||
}
|
||||
|
||||
udpSocket->writeDatagram(buffer, QHostAddress(ip), port);
|
||||
|
||||
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
|
||||
append(0, str);
|
||||
}
|
||||
|
||||
void frmUdpClient::on_btnSave_clicked()
|
||||
{
|
||||
QString data = ui->txtMain->toPlainText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(STRDATETIME);
|
||||
QFile file(fileName);
|
||||
if (file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
file.write(data.toUtf8());
|
||||
file.close();
|
||||
}
|
||||
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmUdpClient::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmUdpClient::on_btnSend_clicked()
|
||||
{
|
||||
QString data = ui->cboxData->currentText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendData(App::UdpServerIP, App::UdpServerPort, data);
|
||||
}
|
||||
41
nettool/form/frmudpclient.h
Normal file
41
nettool/form/frmudpclient.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef FRMUDPCLIENT_H
|
||||
#define FRMUDPCLIENT_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtNetwork>
|
||||
|
||||
namespace Ui {
|
||||
class frmUdpClient;
|
||||
}
|
||||
|
||||
class frmUdpClient : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmUdpClient(QWidget *parent = 0);
|
||||
~frmUdpClient();
|
||||
|
||||
private:
|
||||
Ui::frmUdpClient *ui;
|
||||
|
||||
QUdpSocket *udpSocket;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void changeTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
void readData();
|
||||
void sendData(const QString &ip, int port, const QString &data);
|
||||
|
||||
private slots:
|
||||
void on_btnSave_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnSend_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMUDPCLIENT_H
|
||||
194
nettool/form/frmudpclient.ui
Normal file
194
nettool/form/frmudpclient.ui
Normal file
@@ -0,0 +1,194 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmUdpClient</class>
|
||||
<widget class="QWidget" name="frmUdpClient">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QTextEdit" name="txtMain">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" rowspan="2">
|
||||
<widget class="QFrame" name="frame">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>170</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>170</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Box</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckHexReceive">
|
||||
<property name="text">
|
||||
<string>16进制接收</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckHexSend">
|
||||
<property name="text">
|
||||
<string>16进制发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckAscii">
|
||||
<property name="text">
|
||||
<string>Ascii控制字符</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckShow">
|
||||
<property name="text">
|
||||
<string>暂停显示</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckDebug">
|
||||
<property name="text">
|
||||
<string>模拟设备</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckAutoSend">
|
||||
<property name="text">
|
||||
<string>定时发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxInterval"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labServerIP">
|
||||
<property name="text">
|
||||
<string>远程地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtServerIP"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labServerPort">
|
||||
<property name="text">
|
||||
<string>远程端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtServerPort"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnSave">
|
||||
<property name="text">
|
||||
<string>保存</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnClear">
|
||||
<property name="text">
|
||||
<string>清空</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<layout class="QHBoxLayout" name="layUdpServer">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxData">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnSend">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>80</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>80</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@@ -6,6 +6,7 @@ frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdp
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initIP();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
@@ -24,6 +25,39 @@ void frmUdpServer::initForm()
|
||||
|
||||
ui->cboxInterval->addItems(App::Intervals);
|
||||
ui->cboxData->addItems(App::Datas);
|
||||
QUIHelper::setLabStyle(ui->labCount, 3);
|
||||
}
|
||||
|
||||
void frmUdpServer::initIP()
|
||||
{
|
||||
//获取本机所有IP
|
||||
QStringList ips;
|
||||
QList<QNetworkInterface> netInterfaces = QNetworkInterface::allInterfaces();
|
||||
foreach(const QNetworkInterface &netInterface, netInterfaces) {
|
||||
//移除虚拟机和抓包工具的虚拟网卡
|
||||
QString humanReadableName = netInterface.humanReadableName().toLower();
|
||||
if(humanReadableName.startsWith("vmware network adapter") || humanReadableName.startsWith("npcap loopback adapter")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//过滤当前网络接口
|
||||
bool flag = (netInterface.flags() == (QNetworkInterface::IsUp | QNetworkInterface::IsRunning | QNetworkInterface::CanBroadcast | QNetworkInterface::CanMulticast));
|
||||
if(flag) {
|
||||
QList<QNetworkAddressEntry> addrs = netInterface.addressEntries();
|
||||
foreach(QNetworkAddressEntry addr, addrs) {
|
||||
//只取出IPV4的地址
|
||||
if(addr.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
||||
QString ip4 = addr.ip().toString();
|
||||
if(ip4 != "127.0.0.1") {
|
||||
ips.append(ip4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ui->cboxListenIP->addItems(ips);
|
||||
ui->cboxListenIP->addItem("127.0.0.1");
|
||||
}
|
||||
|
||||
void frmUdpServer::initConfig()
|
||||
@@ -46,17 +80,16 @@ void frmUdpServer::initConfig()
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalUdpServer)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerIP->setText(App::UdpServerIP);
|
||||
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerPort->setText(QString::number(App::UdpServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(App::UdpListenIP));
|
||||
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort->setText(QString::number(App::UdpListenPort));
|
||||
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
timer->setInterval(App::IntervalUdpServer);
|
||||
App::AutoSendUdpServer ? timer->start() : timer->stop();
|
||||
ui->ckSelectAll->setChecked(App::SelectAllUdpServer);
|
||||
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmUdpServer::saveConfig()
|
||||
@@ -67,13 +100,26 @@ void frmUdpServer::saveConfig()
|
||||
App::DebugUdpServer = ui->ckDebug->isChecked();
|
||||
App::AutoSendUdpServer = ui->ckAutoSend->isChecked();
|
||||
App::IntervalUdpServer = ui->cboxInterval->currentText().toInt();
|
||||
App::UdpServerIP = ui->txtServerIP->text().trimmed();
|
||||
App::UdpServerPort = ui->txtServerPort->text().trimmed().toInt();
|
||||
App::UdpListenIP = ui->cboxListenIP->currentText();
|
||||
App::UdpListenPort = ui->txtListenPort->text().trimmed().toInt();
|
||||
App::SelectAllUdpServer = ui->ckSelectAll->isChecked();
|
||||
App::writeConfig();
|
||||
|
||||
this->changeTimer();
|
||||
}
|
||||
|
||||
void frmUdpServer::changeTimer()
|
||||
{
|
||||
timer->setInterval(App::IntervalUdpServer);
|
||||
App::AutoSendUdpServer ? timer->start() : timer->stop();
|
||||
if (App::AutoSendUdpServer) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpServer::append(int type, const QString &data, bool clear)
|
||||
@@ -136,12 +182,14 @@ void frmUdpServer::readData()
|
||||
}
|
||||
|
||||
QString ip = host.toString();
|
||||
ip = ip.replace("::ffff:", "");
|
||||
if (ip.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer);
|
||||
append(1, str);
|
||||
clientConnected(ip, port);
|
||||
|
||||
if (App::DebugUdpServer) {
|
||||
int count = App::Keys.count();
|
||||
@@ -170,17 +218,27 @@ void frmUdpServer::sendData(const QString &ip, int port, const QString &data)
|
||||
|
||||
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
|
||||
append(0, str);
|
||||
}
|
||||
|
||||
void frmUdpServer::clientConnected(const QString &ip, int port)
|
||||
{
|
||||
//先过滤重复的
|
||||
QString str = QString("%1:%2").arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget->count(); i++) {
|
||||
QString s = ui->listWidget->item(i)->text();
|
||||
if (str == s) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ui->listWidget->addItem(str);
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmUdpServer::on_btnListen_clicked()
|
||||
{
|
||||
if (ui->btnListen->text() == "监听") {
|
||||
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
|
||||
bool ok = udpSocket->bind(QHostAddress::AnyIPv4, App::UdpListenPort);
|
||||
#else
|
||||
bool ok = udpSocket->bind(QHostAddress::Any, App::UdpListenPort);
|
||||
#endif
|
||||
bool ok = udpSocket->bind(QHostAddress(App::UdpListenIP), App::UdpListenPort);
|
||||
if (ok) {
|
||||
ui->btnListen->setText("关闭");
|
||||
append(0, "监听成功");
|
||||
@@ -220,5 +278,18 @@ void frmUdpServer::on_btnSend_clicked()
|
||||
return;
|
||||
}
|
||||
|
||||
sendData(App::UdpServerIP, App::UdpServerPort, data);
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
for (int i = 0; i < ui->listWidget->count(); i++) {
|
||||
QString str = ui->listWidget->item(i)->text();
|
||||
QStringList list = str.split(":");
|
||||
sendData(list.at(0), list.at(1).toInt(), data);
|
||||
}
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
sendData(list.at(0), list.at(1).toInt(), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,12 +24,15 @@ private:
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initIP();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void changeTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
void readData();
|
||||
void sendData(const QString &ip, int port, const QString &data);
|
||||
void clientConnected(const QString &ip, int port);
|
||||
|
||||
private slots:
|
||||
void on_btnListen_clicked();
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckHexReceive">
|
||||
<property name="text">
|
||||
@@ -88,24 +88,14 @@
|
||||
<widget class="QComboBox" name="cboxInterval"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labServerIP">
|
||||
<widget class="QLabel" name="labListenIP">
|
||||
<property name="text">
|
||||
<string>远程IP地址</string>
|
||||
<string>监听地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtServerIP"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labServerPort">
|
||||
<property name="text">
|
||||
<string>远程端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtServerPort"/>
|
||||
<widget class="QComboBox" name="cboxListenIP"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labListenPort">
|
||||
@@ -139,17 +129,36 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<widget class="QLabel" name="labCount">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
<width>0</width>
|
||||
<height>25</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Box</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>共 0 个客户端</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QListWidget" name="listWidget"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="ckSelectAll">
|
||||
<property name="text">
|
||||
<string>对所有客户端发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
@@ -206,6 +215,23 @@
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>ckHexReceive</tabstop>
|
||||
<tabstop>ckHexSend</tabstop>
|
||||
<tabstop>ckAscii</tabstop>
|
||||
<tabstop>ckShow</tabstop>
|
||||
<tabstop>ckDebug</tabstop>
|
||||
<tabstop>ckAutoSend</tabstop>
|
||||
<tabstop>cboxInterval</tabstop>
|
||||
<tabstop>cboxListenIP</tabstop>
|
||||
<tabstop>txtListenPort</tabstop>
|
||||
<tabstop>btnListen</tabstop>
|
||||
<tabstop>btnSave</tabstop>
|
||||
<tabstop>btnClear</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
||||
Reference in New Issue
Block a user