更新网络调试助手

master
feiyangqingyun 2019-12-05 16:01:43 +08:00
parent 847355b4ae
commit e176892393
24 changed files with 802 additions and 111 deletions

View File

@ -21,9 +21,19 @@ bool App::HexReceiveTcpServer = false;
bool App::AsciiTcpServer = false;
bool App::DebugTcpServer = false;
bool App::AutoSendTcpServer = false;
bool App::SelectAllTcpServer = false;
int App::IntervalTcpServer = 1000;
QString App::TcpListenIP = "127.0.0.1";
int App::TcpListenPort = 6000;
bool App::SelectAllTcpServer = false;
bool App::HexSendUdpClient = false;
bool App::HexReceiveUdpClient = false;
bool App::AsciiUdpClient = false;
bool App::DebugUdpClient = false;
bool App::AutoSendUdpClient = false;
int App::IntervalUdpClient = 1000;
QString App::UdpServerIP = "127.0.0.1";
int App::UdpServerPort = 6000;
bool App::HexSendUdpServer = false;
bool App::HexReceiveUdpServer = false;
@ -31,9 +41,9 @@ bool App::AsciiUdpServer = false;
bool App::DebugUdpServer = false;
bool App::AutoSendUdpServer = false;
int App::IntervalUdpServer = 1000;
QString App::UdpListenIP = "127.0.0.1";
int App::UdpListenPort = 6000;
QString App::UdpServerIP = "127.0.0.1";
int App::UdpServerPort = 6000;
bool App::SelectAllUdpServer = false;
void App::readConfig()
{
@ -63,10 +73,22 @@ void App::readConfig()
App::HexReceiveTcpServer = set.value("HexReceiveTcpServer").toBool();
App::AsciiTcpServer = set.value("AsciiTcpServer").toBool();
App::DebugTcpServer = set.value("DebugTcpServer").toBool();
App::AutoSendTcpServer = set.value("AutoSendTcpServer").toBool();
App::SelectAllTcpServer = set.value("SelectAllTcpServer").toBool();
App::IntervalTcpServer = set.value("IntervalTcpServer").toInt();
App::AutoSendTcpServer = set.value("AutoSendTcpServer").toBool();
App::IntervalTcpServer = set.value("IntervalTcpServer").toInt();
App::TcpListenIP = set.value("TcpListenIP").toString();
App::TcpListenPort = set.value("TcpListenPort").toInt();
App::SelectAllTcpServer = set.value("SelectAllTcpServer").toBool();
set.endGroup();
set.beginGroup("UdpClientConfig");
App::HexSendUdpClient = set.value("HexSendUdpClient").toBool();
App::HexReceiveUdpClient = set.value("HexReceiveUdpClient").toBool();
App::AsciiUdpClient = set.value("AsciiUdpClient").toBool();
App::DebugUdpClient = set.value("DebugUdpClient").toBool();
App::AutoSendUdpClient = set.value("AutoSendUdpClient").toBool();
App::IntervalUdpClient = set.value("IntervalUdpClient").toInt();
App::UdpServerIP = set.value("UdpServerIP").toString();
App::UdpServerPort = set.value("UdpServerPort").toInt();
set.endGroup();
set.beginGroup("UdpServerConfig");
@ -75,10 +97,10 @@ void App::readConfig()
App::AsciiUdpServer = set.value("AsciiUdpServer").toBool();
App::DebugUdpServer = set.value("DebugUdpServer").toBool();
App::AutoSendUdpServer = set.value("AutoSendUdpServer").toBool();
App::IntervalUdpServer = set.value("IntervalUdpServer").toInt();
App::UdpServerIP = set.value("UdpServerIP").toString();
App::UdpServerPort = set.value("UdpServerPort").toInt();
App::IntervalUdpServer = set.value("IntervalUdpServer").toInt();
App::UdpListenIP = set.value("UdpListenIP").toString();
App::UdpListenPort = set.value("UdpListenPort").toInt();
App::SelectAllUdpServer = set.value("SelectAllUdpServer").toBool();
set.endGroup();
}
@ -104,10 +126,21 @@ void App::writeConfig()
set.setValue("HexSendTcpServer", App::HexSendTcpServer);
set.setValue("HexReceiveTcpServer", App::HexReceiveTcpServer);
set.setValue("DebugTcpServer", App::DebugTcpServer);
set.setValue("AutoSendTcpServer", App::AutoSendTcpServer);
set.setValue("SelectAllTcpServer", App::SelectAllTcpServer);
set.setValue("IntervalTcpServer", App::IntervalTcpServer);
set.setValue("AutoSendTcpServer", App::AutoSendTcpServer);
set.setValue("IntervalTcpServer", App::IntervalTcpServer);
set.setValue("TcpListenIP", App::TcpListenIP);
set.setValue("TcpListenPort", App::TcpListenPort);
set.setValue("SelectAllTcpServer", App::SelectAllTcpServer);
set.endGroup();
set.beginGroup("UdpClientConfig");
set.setValue("HexSendUdpClient", App::HexSendUdpClient);
set.setValue("HexReceiveUdpClient", App::HexReceiveUdpClient);
set.setValue("DebugUdpClient", App::DebugUdpClient);
set.setValue("AutoSendUdpClient", App::AutoSendUdpClient);
set.setValue("IntervalUdpClient", App::IntervalUdpClient);
set.setValue("UdpServerIP", App::UdpServerIP);
set.setValue("UdpServerPort", App::UdpServerPort);
set.endGroup();
set.beginGroup("UdpServerConfig");
@ -115,11 +148,11 @@ void App::writeConfig()
set.setValue("HexReceiveUdpServer", App::HexReceiveUdpServer);
set.setValue("DebugUdpServer", App::DebugUdpServer);
set.setValue("AutoSendUdpServer", App::AutoSendUdpServer);
set.setValue("IntervalUdpServer", App::IntervalUdpServer);
set.setValue("UdpServerIP", App::UdpServerIP);
set.setValue("UdpServerPort", App::UdpServerPort);
set.setValue("IntervalUdpServer", App::IntervalUdpServer);
set.setValue("UdpListenIP", App::UdpListenIP);
set.setValue("UdpListenPort", App::UdpListenPort);
set.endGroup();
set.setValue("SelectAllUdpServer", App::SelectAllUdpServer);
set.endGroup();
}
void App::newConfig()

View File

@ -28,9 +28,20 @@ public:
static bool AsciiTcpServer; //ASCII模式
static bool DebugTcpServer; //启用数据调试
static bool AutoSendTcpServer; //自动发送数据
static bool SelectAllTcpServer; //选中所有
static int IntervalTcpServer; //发送数据间隔
static int IntervalTcpServer; //发送数据间隔
static QString TcpListenIP; //监听地址
static int TcpListenPort; //监听端口
static bool SelectAllTcpServer; //选中所有
//UDP客户端配置参数
static bool HexSendUdpClient; //16进制发送
static bool HexReceiveUdpClient; //16进制接收
static bool AsciiUdpClient; //ASCII模式
static bool DebugUdpClient; //启用数据调试
static bool AutoSendUdpClient; //自动发送数据
static int IntervalUdpClient; //发送数据间隔
static QString UdpServerIP; //服务器IP
static int UdpServerPort; //服务器端口
//UDP服务器配置参数
static bool HexSendUdpServer; //16进制发送
@ -38,10 +49,10 @@ public:
static bool AsciiUdpServer; //ASCII模式
static bool DebugUdpServer; //启用数据调试
static bool AutoSendUdpServer; //自动发送数据
static int IntervalUdpServer; //发送数据间隔
static QString UdpServerIP; //服务器IP
static int UdpServerPort; //服务器端口
static int IntervalUdpServer; //发送数据间隔
static QString UdpListenIP; //监听地址
static int UdpListenPort; //监听端口
static bool SelectAllUdpServer; //选中所有
//读写配置参数及其他操作
static void readConfig(); //读取配置参数

View File

@ -17,7 +17,7 @@ QUIWidget::QUIWidget(QWidget *parent) : QDialog(parent)
}
QUIWidget::~QUIWidget()
{
{
}
bool QUIWidget::eventFilter(QObject *watched, QEvent *event)
@ -369,7 +369,7 @@ void QUIWidget::setIconMain(const QChar &str, quint32 size)
void QUIWidget::setPixmap(QUIWidget::Widget widget, const QString &file, const QSize &size)
{
//按照宽高比自动缩放
QPixmap pix = QPixmap(file);
QPixmap pix = QPixmap(file);
pix = pix.scaled(size, Qt::KeepAspectRatio);
if (widget == QUIWidget::Lab_Ico) {
this->labIco->setPixmap(pix);
@ -454,12 +454,12 @@ void QUIWidget::setExitAll(bool exitAll)
void QUIWidget::setMainWidget(QWidget *mainWidget)
{
//一个QUI窗体对象只能设置一个主窗体
if (this->mainWidget == 0) {
if (this->mainWidget == 0) {
//将子窗体添加到布局
this->widget->layout()->addWidget(mainWidget);
//自动设置大小
resize(mainWidget->width(), mainWidget->height() + this->widgetTitle->height());
this->mainWidget = mainWidget;
this->mainWidget = mainWidget;
}
}
@ -917,6 +917,7 @@ void QUITipBox::initControl()
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(labTime->sizePolicy().hasHeightForWidth());
labTime->setSizePolicy(sizePolicy1);
labTime->setAlignment(Qt::AlignCenter);
horizontalLayout2->addWidget(labTime);
widgetMenu = new QWidget(widgetTitle);
@ -943,6 +944,7 @@ void QUITipBox::initControl()
btnMenu_Close->setFocusPolicy(Qt::NoFocus);
btnMenu_Close->setFlat(true);
widgetTitle->setMinimumHeight(TitleMinSize);
horizontalLayout->addWidget(btnMenu_Close);
horizontalLayout2->addWidget(widgetMenu);
verticalLayout->addWidget(widgetTitle);
@ -1039,7 +1041,7 @@ void QUITipBox::setTip(const QString &title, const QString &tip, bool fullScreen
this->labInfo->setAlignment(center ? Qt::AlignCenter : Qt::AlignLeft);
this->setWindowTitle(this->labTitle->text());
QRect rect = fullScreen ? qApp->desktop()->availableGeometry() : qApp->desktop()->geometry();
QRect rect = fullScreen ? qApp->desktop()->geometry() : qApp->desktop()->availableGeometry();
int width = rect.width();
int height = rect.height();
int x = width - this->width();

View File

@ -89,6 +89,7 @@ void TcpServer::incomingConnection(int handle)
connect(client, SIGNAL(receiveData(QString, int, QString)), this, SIGNAL(receiveData(QString, int, QString)));
QString ip = client->peerAddress().toString();
ip = ip.replace("::ffff:", "");
int port = client->peerPort();
client->setIP(ip);
client->setPort(port);
@ -113,12 +114,7 @@ void TcpServer::disconnected()
bool TcpServer::start()
{
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
bool ok = listen(QHostAddress::AnyIPv4, App::TcpListenPort);
#else
bool ok = listen(QHostAddress::Any, App::TcpListenPort);
#endif
bool ok = listen(QHostAddress(App::TcpListenIP), App::TcpListenPort);
return ok;
}

View File

@ -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

View File

@ -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/>

View File

@ -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)

View File

@ -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();

View File

@ -102,7 +102,7 @@
<item>
<widget class="QLabel" name="labServerIP">
<property name="text">
<string>服务器IP地址</string>
<string>服务器地址</string>
</property>
</widget>
</item>

View File

@ -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()

View File

@ -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);

View File

@ -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>

View 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);
}

View 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

View 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>

View File

@ -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);
}
}
}

View File

@ -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();

View File

@ -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>

View File

@ -24,7 +24,7 @@ int main(int argc, char *argv[])
App::readDeviceData();
frmMain w;
w.setWindowTitle(QString("网络调试助手V2019 本机IP: %1 QQ: 517216493").arg(QUIHelper::getLocalIP()));
w.setWindowTitle(QString("网络调试助手V2020 本机IP: %1 QQ: 517216493").arg(QUIHelper::getLocalIP()));
w.show();
return a.exec();

View File

@ -11,7 +11,7 @@
6可对单个在线连接发送数据也可勾选全部进行发送。
7支持多个客户端连接并发。
8采用单线程。
9四种模式tcp服务器、tcp客户端、udp服务器、udp客户端
9四种模式tcp客户端、tcp服务器、udp客户端、udp服务器
编译后请将源码下的file目录中的所有文件复制到可执行文件同一目录。
如果有更好的建议或者意见请Q我(517216493),谢谢!

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB