彻底改版2.0
This commit is contained in:
28
tool/nettool/form/form.pri
Normal file
28
tool/nettool/form/form.pri
Normal file
@@ -0,0 +1,28 @@
|
||||
FORMS += $$PWD/frmmain.ui
|
||||
FORMS += $$PWD/frmtcpclient.ui
|
||||
FORMS += $$PWD/frmtcpserver.ui
|
||||
FORMS += $$PWD/frmudpclient.ui
|
||||
FORMS += $$PWD/frmudpserver.ui
|
||||
|
||||
HEADERS += $$PWD/frmmain.h
|
||||
HEADERS += $$PWD/frmtcpclient.h
|
||||
HEADERS += $$PWD/frmtcpserver.h
|
||||
HEADERS += $$PWD/frmudpclient.h
|
||||
HEADERS += $$PWD/frmudpserver.h
|
||||
|
||||
SOURCES += $$PWD/frmmain.cpp
|
||||
SOURCES += $$PWD/frmtcpclient.cpp
|
||||
SOURCES += $$PWD/frmtcpserver.cpp
|
||||
SOURCES += $$PWD/frmudpclient.cpp
|
||||
SOURCES += $$PWD/frmudpserver.cpp
|
||||
|
||||
contains(DEFINES, websocket) {
|
||||
FORMS += $$PWD/frmwebclient.ui
|
||||
FORMS += $$PWD/frmwebserver.ui
|
||||
|
||||
HEADERS += $$PWD/frmwebclient.h
|
||||
HEADERS += $$PWD/frmwebserver.h
|
||||
|
||||
SOURCES += $$PWD/frmwebclient.cpp
|
||||
SOURCES += $$PWD/frmwebserver.cpp
|
||||
}
|
||||
51
tool/nettool/form/frmmain.cpp
Normal file
51
tool/nettool/form/frmmain.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "frmmain.h"
|
||||
#include "ui_frmmain.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
#include "frmtcpclient.h"
|
||||
#include "frmtcpserver.h"
|
||||
#include "frmudpclient.h"
|
||||
#include "frmudpserver.h"
|
||||
#ifdef websocket
|
||||
#include "frmwebclient.h"
|
||||
#include "frmwebserver.h"
|
||||
#endif
|
||||
|
||||
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
frmMain::~frmMain()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmMain::initForm()
|
||||
{
|
||||
ui->tabWidget->addTab(new frmTcpClient, "TCP客户端");
|
||||
ui->tabWidget->addTab(new frmTcpServer, "TCP服务端");
|
||||
ui->tabWidget->addTab(new frmUdpClient, "UDP客户端");
|
||||
ui->tabWidget->addTab(new frmUdpServer, "UDP服务端");
|
||||
#ifdef websocket
|
||||
ui->tabWidget->addTab(new frmWebClient, "WEB客户端");
|
||||
ui->tabWidget->addTab(new frmWebServer, "WEB服务端");
|
||||
#endif
|
||||
#ifdef emsdk
|
||||
AppConfig::CurrentIndex = 4;
|
||||
#endif
|
||||
}
|
||||
|
||||
void frmMain::initConfig()
|
||||
{
|
||||
ui->tabWidget->setCurrentIndex(AppConfig::CurrentIndex);
|
||||
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(saveConfig()));
|
||||
}
|
||||
|
||||
void frmMain::saveConfig()
|
||||
{
|
||||
AppConfig::CurrentIndex = ui->tabWidget->currentIndex();
|
||||
AppConfig::writeConfig();
|
||||
}
|
||||
27
tool/nettool/form/frmmain.h
Normal file
27
tool/nettool/form/frmmain.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef FRMMAIN_H
|
||||
#define FRMMAIN_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class frmMain;
|
||||
}
|
||||
|
||||
class frmMain : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmMain(QWidget *parent = 0);
|
||||
~frmMain();
|
||||
|
||||
private:
|
||||
Ui::frmMain *ui;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
};
|
||||
|
||||
#endif // FRMMAIN_H
|
||||
46
tool/nettool/form/frmmain.ui
Normal file
46
tool/nettool/form/frmmain.ui
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmMain</class>
|
||||
<widget class="QWidget" name="frmMain">
|
||||
<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="QVBoxLayout" name="verticalLayout">
|
||||
<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="QTabWidget" name="tabWidget">
|
||||
<property name="styleSheet">
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="tabPosition">
|
||||
<enum>QTabWidget::South</enum>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>-1</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
282
tool/nettool/form/frmtcpclient.cpp
Normal file
282
tool/nettool/form/frmtcpclient.cpp
Normal file
@@ -0,0 +1,282 @@
|
||||
#include "frmtcpclient.h"
|
||||
#include "ui_frmtcpclient.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
frmTcpClient::frmTcpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcpClient)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
frmTcpClient::~frmTcpClient()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmTcpClient::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmTcpClient::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
isOk = false;
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
socket = new QTcpSocket(this);
|
||||
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
|
||||
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
#else
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
#endif
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
|
||||
//定时器发送数据
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
//填充数据到下拉框
|
||||
ui->cboxInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
AppData::loadIP(ui->cboxBindIP);
|
||||
}
|
||||
|
||||
void frmTcpClient::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSendTcpClient);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceiveTcpClient);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(AppConfig::AsciiTcpClient);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::DebugTcpClient);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSendTcpClient);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalTcpClient)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxBindIP->setCurrentIndex(ui->cboxBindIP->findText(AppConfig::TcpBindIP));
|
||||
connect(ui->cboxBindIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtBindPort->setText(QString::number(AppConfig::TcpBindPort));
|
||||
connect(ui->txtBindPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerIP->setText(AppConfig::TcpServerIP);
|
||||
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerPort->setText(QString::number(AppConfig::TcpServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmTcpClient::saveConfig()
|
||||
{
|
||||
AppConfig::HexSendTcpClient = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceiveTcpClient = ui->ckHexReceive->isChecked();
|
||||
AppConfig::AsciiTcpClient = ui->ckAscii->isChecked();
|
||||
AppConfig::DebugTcpClient = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoSendTcpClient = ui->ckAutoSend->isChecked();
|
||||
AppConfig::IntervalTcpClient = ui->cboxInterval->currentText().toInt();
|
||||
AppConfig::TcpBindIP = ui->cboxBindIP->currentText();
|
||||
AppConfig::TcpBindPort = ui->txtBindPort->text().trimmed().toInt();
|
||||
AppConfig::TcpServerIP = ui->txtServerIP->text().trimmed();
|
||||
AppConfig::TcpServerPort = ui->txtServerPort->text().trimmed().toInt();
|
||||
AppConfig::writeConfig();
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmTcpClient::initTimer()
|
||||
{
|
||||
if (timer->interval() != AppConfig::IntervalTcpClient) {
|
||||
timer->setInterval(AppConfig::IntervalTcpClient);
|
||||
}
|
||||
|
||||
if (AppConfig::AutoSendTcpClient) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpClient::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("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmTcpClient::connected()
|
||||
{
|
||||
isOk = true;
|
||||
ui->btnConnect->setText("断开");
|
||||
append(0, "服务器连接");
|
||||
append(0, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort()));
|
||||
append(0, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
|
||||
}
|
||||
|
||||
void frmTcpClient::disconnected()
|
||||
{
|
||||
isOk = false;
|
||||
ui->btnConnect->setText("连接");
|
||||
append(1, "服务器断开");
|
||||
}
|
||||
|
||||
void frmTcpClient::error()
|
||||
{
|
||||
append(2, socket->errorString());
|
||||
}
|
||||
|
||||
void frmTcpClient::readData()
|
||||
{
|
||||
QByteArray data = socket->readAll();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString buffer;
|
||||
if (AppConfig::HexReceiveTcpClient) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else if (AppConfig::AsciiTcpClient) {
|
||||
buffer = QUIHelperData::byteArrayToAsciiStr(data);
|
||||
} else {
|
||||
buffer = QString(data);
|
||||
}
|
||||
|
||||
append(1, buffer);
|
||||
|
||||
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
|
||||
if (AppConfig::DebugTcpClient) {
|
||||
int count = AppData::Keys.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (AppData::Keys.at(i) == buffer) {
|
||||
sendData(AppData::Values.at(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpClient::sendData(const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (AppConfig::HexSendTcpClient) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else if (AppConfig::AsciiTcpClient) {
|
||||
buffer = QUIHelperData::asciiStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toUtf8();
|
||||
}
|
||||
|
||||
socket->write(buffer);
|
||||
append(0, data);
|
||||
}
|
||||
|
||||
void frmTcpClient::on_btnConnect_clicked()
|
||||
{
|
||||
if (ui->btnConnect->text() == "连接") {
|
||||
//断开所有连接和操作
|
||||
//socket->abort();
|
||||
//绑定网卡和端口
|
||||
//有个后遗症,客户端这边断开连接后还会保持几分钟导致不能重复绑定
|
||||
//如果是服务器断开则可以继续使用
|
||||
//提示 The bound address is already in use
|
||||
//参考 https://www.cnblogs.com/baiduboy/p/7426822.html
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
//socket->bind(QHostAddress(AppConfig::TcpBindIP), AppConfig::TcpBindPort);
|
||||
#endif
|
||||
//连接服务器
|
||||
socket->connectToHost(AppConfig::TcpServerIP, AppConfig::TcpServerPort);
|
||||
} else {
|
||||
socket->abort();
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpClient::on_btnSave_clicked()
|
||||
{
|
||||
QString data = ui->txtMain->toPlainText();
|
||||
AppData::saveData(data);
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmTcpClient::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmTcpClient::on_btnSend_clicked()
|
||||
{
|
||||
if (!isOk) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString data = ui->cboxData->currentText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendData(data);
|
||||
}
|
||||
50
tool/nettool/form/frmtcpclient.h
Normal file
50
tool/nettool/form/frmtcpclient.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef FRMTCPCLIENT_H
|
||||
#define FRMTCPCLIENT_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtNetwork>
|
||||
|
||||
namespace Ui {
|
||||
class frmTcpClient;
|
||||
}
|
||||
|
||||
class frmTcpClient : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmTcpClient(QWidget *parent = 0);
|
||||
~frmTcpClient();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
Ui::frmTcpClient *ui;
|
||||
|
||||
bool isOk;
|
||||
QTcpSocket *socket;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void initTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void connected();
|
||||
void disconnected();
|
||||
void error();
|
||||
void readData();
|
||||
void sendData(const QString &data);
|
||||
|
||||
private slots:
|
||||
void on_btnConnect_clicked();
|
||||
void on_btnSave_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnSend_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMTCPCLIENT_H
|
||||
264
tool/nettool/form/frmtcpclient.ui
Normal file
264
tool/nettool/form/frmtcpclient.ui
Normal file
@@ -0,0 +1,264 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmTcpClient</class>
|
||||
<widget class="QWidget" name="frmTcpClient">
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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>控制字符</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="labBindIP">
|
||||
<property name="text">
|
||||
<string>绑定地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxBindIP"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labBindPort">
|
||||
<property name="text">
|
||||
<string>绑定端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtBindPort"/>
|
||||
</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="btnConnect">
|
||||
<property name="text">
|
||||
<string>连接</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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="spacer">
|
||||
<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="layTcpClient">
|
||||
<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>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</tabstop>
|
||||
<tabstop>ckHexReceive</tabstop>
|
||||
<tabstop>ckHexSend</tabstop>
|
||||
<tabstop>ckAscii</tabstop>
|
||||
<tabstop>ckShow</tabstop>
|
||||
<tabstop>ckDebug</tabstop>
|
||||
<tabstop>ckAutoSend</tabstop>
|
||||
<tabstop>cboxInterval</tabstop>
|
||||
<tabstop>cboxBindIP</tabstop>
|
||||
<tabstop>txtBindPort</tabstop>
|
||||
<tabstop>txtServerIP</tabstop>
|
||||
<tabstop>txtServerPort</tabstop>
|
||||
<tabstop>btnConnect</tabstop>
|
||||
<tabstop>btnSave</tabstop>
|
||||
<tabstop>btnClear</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
270
tool/nettool/form/frmtcpserver.cpp
Normal file
270
tool/nettool/form/frmtcpserver.cpp
Normal file
@@ -0,0 +1,270 @@
|
||||
#include "frmtcpserver.h"
|
||||
#include "ui_frmtcpserver.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcpServer)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
on_btnListen_clicked();
|
||||
}
|
||||
|
||||
frmTcpServer::~frmTcpServer()
|
||||
{
|
||||
//结束的时候停止服务
|
||||
server->stop();
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmTcpServer::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmTcpServer::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
isOk = false;
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
server = new TcpServer(this);
|
||||
connect(server, SIGNAL(connected(QString, int)), this, SLOT(connected(QString, int)));
|
||||
connect(server, SIGNAL(disconnected(QString, int)), this, SLOT(disconnected(QString, int)));
|
||||
connect(server, SIGNAL(error(QString, int, QString)), this, SLOT(error(QString, int, QString)));
|
||||
connect(server, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));
|
||||
connect(server, SIGNAL(receiveData(QString, int, QString)), this, SLOT(receiveData(QString, int, QString)));
|
||||
|
||||
//定时器发送数据
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
//填充数据到下拉框
|
||||
ui->cboxInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
AppData::loadIP(ui->cboxListenIP);
|
||||
}
|
||||
|
||||
void frmTcpServer::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSendTcpServer);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceiveTcpServer);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(AppConfig::AsciiTcpServer);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::DebugTcpServer);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSendTcpServer);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalTcpServer)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(AppConfig::TcpListenIP));
|
||||
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort->setText(QString::number(AppConfig::TcpListenPort));
|
||||
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckSelectAll->setChecked(AppConfig::SelectAllTcpServer);
|
||||
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmTcpServer::saveConfig()
|
||||
{
|
||||
AppConfig::HexSendTcpServer = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceiveTcpServer = ui->ckHexReceive->isChecked();
|
||||
AppConfig::AsciiTcpServer = ui->ckAscii->isChecked();
|
||||
AppConfig::DebugTcpServer = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoSendTcpServer = ui->ckAutoSend->isChecked();
|
||||
AppConfig::IntervalTcpServer = ui->cboxInterval->currentText().toInt();
|
||||
AppConfig::TcpListenIP = ui->cboxListenIP->currentText();
|
||||
AppConfig::TcpListenPort = ui->txtListenPort->text().trimmed().toInt();
|
||||
AppConfig::SelectAllTcpServer = ui->ckSelectAll->isChecked();
|
||||
AppConfig::writeConfig();
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmTcpServer::initTimer()
|
||||
{
|
||||
if (timer->interval() != AppConfig::IntervalTcpServer) {
|
||||
timer->setInterval(AppConfig::IntervalTcpServer);
|
||||
}
|
||||
|
||||
if (AppConfig::AutoSendTcpServer) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpServer::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("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmTcpServer::connected(const QString &ip, int port)
|
||||
{
|
||||
append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端上线"));
|
||||
|
||||
QString str = QString("%1:%2").arg(ip).arg(port);
|
||||
ui->listWidget->addItem(str);
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmTcpServer::disconnected(const QString &ip, int port)
|
||||
{
|
||||
append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端下线"));
|
||||
|
||||
int row = -1;
|
||||
QString str = QString("%1:%2").arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget->count(); i++) {
|
||||
if (ui->listWidget->item(i)->text() == str) {
|
||||
row = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete ui->listWidget->takeItem(row);
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmTcpServer::error(const QString &ip, int port, const QString &error)
|
||||
{
|
||||
append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg(error));
|
||||
}
|
||||
|
||||
void frmTcpServer::sendData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg(data));
|
||||
}
|
||||
|
||||
void frmTcpServer::receiveData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
append(1, QString("[%1:%2] %3").arg(ip).arg(port).arg(data));
|
||||
}
|
||||
|
||||
void frmTcpServer::on_btnListen_clicked()
|
||||
{
|
||||
if (ui->btnListen->text() == "监听") {
|
||||
isOk = server->start();
|
||||
if (isOk) {
|
||||
append(0, "监听成功");
|
||||
ui->btnListen->setText("关闭");
|
||||
}
|
||||
} else {
|
||||
isOk = false;
|
||||
server->stop();
|
||||
ui->btnListen->setText("监听");
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpServer::on_btnSave_clicked()
|
||||
{
|
||||
QString data = ui->txtMain->toPlainText();
|
||||
AppData::saveData(data);
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmTcpServer::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmTcpServer::on_btnSend_clicked()
|
||||
{
|
||||
if (!isOk) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString data = ui->cboxData->currentText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
server->writeData(data);
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
server->writeData(list.at(0), list.at(1).toInt(), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmTcpServer::on_btnRemove_clicked()
|
||||
{
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
server->remove();
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
server->remove(list.at(0), list.at(1).toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
52
tool/nettool/form/frmtcpserver.h
Normal file
52
tool/nettool/form/frmtcpserver.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef FRMTCPSERVER_H
|
||||
#define FRMTCPSERVER_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "tcpserver.h"
|
||||
|
||||
namespace Ui {
|
||||
class frmTcpServer;
|
||||
}
|
||||
|
||||
class frmTcpServer : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmTcpServer(QWidget *parent = 0);
|
||||
~frmTcpServer();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
Ui::frmTcpServer *ui;
|
||||
|
||||
bool isOk;
|
||||
TcpServer *server;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void initTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void connected(const QString &ip, int port);
|
||||
void disconnected(const QString &ip, int port);
|
||||
void error(const QString &ip, int port, const QString &error);
|
||||
|
||||
void sendData(const QString &ip, int port, const QString &data);
|
||||
void receiveData(const QString &ip, int port, const QString &data);
|
||||
|
||||
private slots:
|
||||
void on_btnListen_clicked();
|
||||
void on_btnSave_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnSend_clicked();
|
||||
void on_btnRemove_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMTCPSERVER_H
|
||||
271
tool/nettool/form/frmtcpserver.ui
Normal file
271
tool/nettool/form/frmtcpserver.ui
Normal file
@@ -0,0 +1,271 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmTcpServer</class>
|
||||
<widget class="QWidget" name="frmTcpServer">
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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_3">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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>控制字符</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="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">
|
||||
<string>监听端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtListenPort"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnListen">
|
||||
<property name="text">
|
||||
<string>监听</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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>
|
||||
<widget class="QPushButton" name="btnRemove">
|
||||
<property name="text">
|
||||
<string>移除</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labCount">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>25</height>
|
||||
</size>
|
||||
</property>
|
||||
<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>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<layout class="QHBoxLayout" name="layTcpServer">
|
||||
<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>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</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>btnRemove</tabstop>
|
||||
<tabstop>listWidget</tabstop>
|
||||
<tabstop>ckSelectAll</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
258
tool/nettool/form/frmudpclient.cpp
Normal file
258
tool/nettool/form/frmudpclient.cpp
Normal file
@@ -0,0 +1,258 @@
|
||||
#include "frmudpclient.h"
|
||||
#include "ui_frmudpclient.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
frmUdpClient::frmUdpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdpClient)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
frmUdpClient::~frmUdpClient()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmUdpClient::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmUdpClient::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
socket = new QUdpSocket(this);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
#else
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
#endif
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
|
||||
//定时器发送数据
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
//填充数据到下拉框
|
||||
ui->cboxInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
AppData::loadIP(ui->cboxBindIP);
|
||||
}
|
||||
|
||||
void frmUdpClient::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSendUdpClient);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceiveUdpClient);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(AppConfig::AsciiUdpClient);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::DebugUdpClient);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSendUdpClient);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalUdpClient)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxBindIP->setCurrentIndex(ui->cboxBindIP->findText(AppConfig::UdpBindIP));
|
||||
connect(ui->cboxBindIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtBindPort->setText(QString::number(AppConfig::UdpBindPort));
|
||||
connect(ui->txtBindPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerIP->setText(AppConfig::UdpServerIP);
|
||||
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerPort->setText(QString::number(AppConfig::UdpServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmUdpClient::saveConfig()
|
||||
{
|
||||
AppConfig::HexSendUdpClient = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceiveUdpClient = ui->ckHexReceive->isChecked();
|
||||
AppConfig::AsciiUdpClient = ui->ckAscii->isChecked();
|
||||
AppConfig::DebugUdpClient = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoSendUdpClient = ui->ckAutoSend->isChecked();
|
||||
AppConfig::IntervalUdpClient = ui->cboxInterval->currentText().toInt();
|
||||
AppConfig::UdpBindIP = ui->cboxBindIP->currentText();
|
||||
AppConfig::UdpBindPort = ui->txtBindPort->text().trimmed().toInt();
|
||||
AppConfig::UdpServerIP = ui->txtServerIP->text().trimmed();
|
||||
AppConfig::UdpServerPort = ui->txtServerPort->text().trimmed().toInt();
|
||||
AppConfig::writeConfig();
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmUdpClient::initTimer()
|
||||
{
|
||||
if (timer->interval() != AppConfig::IntervalUdpClient) {
|
||||
timer->setInterval(AppConfig::IntervalUdpClient);
|
||||
}
|
||||
|
||||
if (AppConfig::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("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmUdpClient::error()
|
||||
{
|
||||
append(2, socket->errorString());
|
||||
}
|
||||
|
||||
void frmUdpClient::readData()
|
||||
{
|
||||
QHostAddress host;
|
||||
quint16 port;
|
||||
QByteArray data;
|
||||
QString buffer;
|
||||
|
||||
while (socket->hasPendingDatagrams()) {
|
||||
data.resize(socket->pendingDatagramSize());
|
||||
socket->readDatagram(data.data(), data.size(), &host, &port);
|
||||
|
||||
if (AppConfig::HexReceiveUdpClient) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else if (AppConfig::AsciiUdpClient) {
|
||||
buffer = QUIHelperData::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 (AppConfig::DebugUdpClient) {
|
||||
int count = AppData::Keys.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (AppData::Keys.at(i) == buffer) {
|
||||
sendData(ip, port, AppData::Values.at(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpClient::sendData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (AppConfig::HexSendUdpClient) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else if (AppConfig::AsciiUdpClient) {
|
||||
buffer = QUIHelperData::asciiStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toUtf8();
|
||||
}
|
||||
|
||||
//绑定网卡和端口,没有绑定过才需要绑定
|
||||
//采用端口是否一样来判断是为了方便可以直接动态绑定切换端口
|
||||
if (socket->localPort() != AppConfig::UdpBindPort) {
|
||||
socket->abort();
|
||||
socket->bind(QHostAddress(AppConfig::UdpBindIP), AppConfig::UdpBindPort);
|
||||
}
|
||||
|
||||
//指定地址和端口发送数据
|
||||
socket->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();
|
||||
AppData::saveData(data);
|
||||
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(AppConfig::UdpServerIP, AppConfig::UdpServerPort, data);
|
||||
}
|
||||
46
tool/nettool/form/frmudpclient.h
Normal file
46
tool/nettool/form/frmudpclient.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#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();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
Ui::frmUdpClient *ui;
|
||||
|
||||
QUdpSocket *socket;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void initTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void error();
|
||||
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
|
||||
256
tool/nettool/form/frmudpclient.ui
Normal file
256
tool/nettool/form/frmudpclient.ui
Normal file
@@ -0,0 +1,256 @@
|
||||
<?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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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>控制字符</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="labBindIP">
|
||||
<property name="text">
|
||||
<string>绑定地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxBindIP"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labBindPort">
|
||||
<property name="text">
|
||||
<string>绑定端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtBindPort"/>
|
||||
</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>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</tabstop>
|
||||
<tabstop>ckHexReceive</tabstop>
|
||||
<tabstop>ckHexSend</tabstop>
|
||||
<tabstop>ckAscii</tabstop>
|
||||
<tabstop>ckShow</tabstop>
|
||||
<tabstop>ckDebug</tabstop>
|
||||
<tabstop>ckAutoSend</tabstop>
|
||||
<tabstop>cboxInterval</tabstop>
|
||||
<tabstop>cboxBindIP</tabstop>
|
||||
<tabstop>txtBindPort</tabstop>
|
||||
<tabstop>txtServerIP</tabstop>
|
||||
<tabstop>txtServerPort</tabstop>
|
||||
<tabstop>btnSave</tabstop>
|
||||
<tabstop>btnClear</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
299
tool/nettool/form/frmudpserver.cpp
Normal file
299
tool/nettool/form/frmudpserver.cpp
Normal file
@@ -0,0 +1,299 @@
|
||||
#include "frmudpserver.h"
|
||||
#include "ui_frmudpserver.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdpServer)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
on_btnListen_clicked();
|
||||
}
|
||||
|
||||
frmUdpServer::~frmUdpServer()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmUdpServer::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmUdpServer::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
socket = new QUdpSocket(this);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
#else
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
#endif
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
|
||||
//定时器发送数据
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
//填充数据到下拉框
|
||||
ui->cboxInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
AppData::loadIP(ui->cboxListenIP);
|
||||
}
|
||||
|
||||
void frmUdpServer::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSendUdpServer);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceiveUdpServer);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(AppConfig::AsciiUdpServer);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::DebugUdpServer);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSendUdpServer);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalUdpServer)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(AppConfig::UdpListenIP));
|
||||
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort->setText(QString::number(AppConfig::UdpListenPort));
|
||||
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckSelectAll->setChecked(AppConfig::SelectAllUdpServer);
|
||||
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmUdpServer::saveConfig()
|
||||
{
|
||||
AppConfig::HexSendUdpServer = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceiveUdpServer = ui->ckHexReceive->isChecked();
|
||||
AppConfig::AsciiUdpServer = ui->ckAscii->isChecked();
|
||||
AppConfig::DebugUdpServer = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoSendUdpServer = ui->ckAutoSend->isChecked();
|
||||
AppConfig::IntervalUdpServer = ui->cboxInterval->currentText().toInt();
|
||||
AppConfig::UdpListenIP = ui->cboxListenIP->currentText();
|
||||
AppConfig::UdpListenPort = ui->txtListenPort->text().trimmed().toInt();
|
||||
AppConfig::SelectAllUdpServer = ui->ckSelectAll->isChecked();
|
||||
AppConfig::writeConfig();
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmUdpServer::initTimer()
|
||||
{
|
||||
if (timer->interval() != AppConfig::IntervalUdpServer) {
|
||||
timer->setInterval(AppConfig::IntervalUdpServer);
|
||||
}
|
||||
|
||||
if (AppConfig::AutoSendUdpServer) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpServer::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("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmUdpServer::error()
|
||||
{
|
||||
append(2, socket->errorString());
|
||||
}
|
||||
|
||||
void frmUdpServer::readData()
|
||||
{
|
||||
QHostAddress host;
|
||||
quint16 port;
|
||||
QByteArray data;
|
||||
QString buffer;
|
||||
|
||||
while (socket->hasPendingDatagrams()) {
|
||||
data.resize(socket->pendingDatagramSize());
|
||||
socket->readDatagram(data.data(), data.size(), &host, &port);
|
||||
|
||||
if (AppConfig::HexReceiveUdpServer) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else if (AppConfig::AsciiUdpServer) {
|
||||
buffer = QUIHelperData::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);
|
||||
|
||||
//先过滤重复的
|
||||
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()));
|
||||
|
||||
if (AppConfig::DebugUdpServer) {
|
||||
int count = AppData::Keys.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (AppData::Keys.at(i) == buffer) {
|
||||
sendData(ip, port, AppData::Values.at(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpServer::sendData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (AppConfig::HexSendUdpServer) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else if (AppConfig::AsciiUdpServer) {
|
||||
buffer = QUIHelperData::asciiStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toUtf8();
|
||||
}
|
||||
|
||||
socket->writeDatagram(buffer, QHostAddress(ip), port);
|
||||
|
||||
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
|
||||
append(0, str);
|
||||
}
|
||||
|
||||
void frmUdpServer::on_btnListen_clicked()
|
||||
{
|
||||
if (ui->btnListen->text() == "监听") {
|
||||
bool ok = socket->bind(QHostAddress(AppConfig::UdpListenIP), AppConfig::UdpListenPort);
|
||||
if (ok) {
|
||||
ui->btnListen->setText("关闭");
|
||||
append(0, "监听成功");
|
||||
}
|
||||
} else {
|
||||
socket->abort();
|
||||
ui->btnListen->setText("监听");
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpServer::on_btnSave_clicked()
|
||||
{
|
||||
QString data = ui->txtMain->toPlainText();
|
||||
AppData::saveData(data);
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmUdpServer::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmUdpServer::on_btnSend_clicked()
|
||||
{
|
||||
QString data = ui->cboxData->currentText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmUdpServer::on_btnRemove_clicked()
|
||||
{
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
ui->listWidget->clear();
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
delete ui->listWidget->takeItem(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
48
tool/nettool/form/frmudpserver.h
Normal file
48
tool/nettool/form/frmudpserver.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef FRMUDPSERVER_H
|
||||
#define FRMUDPSERVER_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtNetwork>
|
||||
|
||||
namespace Ui {
|
||||
class frmUdpServer;
|
||||
}
|
||||
|
||||
class frmUdpServer : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmUdpServer(QWidget *parent = 0);
|
||||
~frmUdpServer();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
Ui::frmUdpServer *ui;
|
||||
|
||||
QUdpSocket *socket;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void initTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void error();
|
||||
void readData();
|
||||
void sendData(const QString &ip, int port, const QString &data);
|
||||
|
||||
private slots:
|
||||
void on_btnListen_clicked();
|
||||
void on_btnSave_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnSend_clicked();
|
||||
void on_btnRemove_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMUDPSERVER_H
|
||||
270
tool/nettool/form/frmudpserver.ui
Normal file
270
tool/nettool/form/frmudpserver.ui
Normal file
@@ -0,0 +1,270 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmUdpServer</class>
|
||||
<widget class="QWidget" name="frmUdpServer">
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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>控制字符</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="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">
|
||||
<string>监听端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtListenPort"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnListen">
|
||||
<property name="text">
|
||||
<string>监听</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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>
|
||||
<widget class="QPushButton" name="btnRemove">
|
||||
<property name="text">
|
||||
<string>移除</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labCount">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>25</height>
|
||||
</size>
|
||||
</property>
|
||||
<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>
|
||||
</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>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</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>listWidget</tabstop>
|
||||
<tabstop>ckSelectAll</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
278
tool/nettool/form/frmwebclient.cpp
Normal file
278
tool/nettool/form/frmwebclient.cpp
Normal file
@@ -0,0 +1,278 @@
|
||||
#include "frmwebclient.h"
|
||||
#include "ui_frmwebclient.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
frmWebClient::frmWebClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmWebClient)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
}
|
||||
|
||||
frmWebClient::~frmWebClient()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmWebClient::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmWebClient::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
isOk = false;
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
socket = new QWebSocket("WebSocket", QWebSocketProtocol::VersionLatest, this);
|
||||
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
|
||||
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
|
||||
//暂时使用前面两个信号,部分系统上后面两个信号Qt没实现,目前测试到5.15.2
|
||||
//在win上如果两组信号都关联了则都会触发,另外一组信号就是多个参数表示是否是最后一个数据包
|
||||
connect(socket, SIGNAL(textMessageReceived(QString)), this, SLOT(textMessageReceived(QString)));
|
||||
connect(socket, SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(binaryMessageReceived(QByteArray)));
|
||||
//connect(socket, SIGNAL(textFrameReceived(QString, bool)), this, SLOT(textFrameReceived(QString, bool)));
|
||||
//connect(socket, SIGNAL(binaryFrameReceived(QByteArray, bool)), this, SLOT(binaryFrameReceived(QByteArray, bool)));
|
||||
|
||||
//定时器发送数据
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
//填充数据到下拉框
|
||||
ui->cboxInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
}
|
||||
|
||||
void frmWebClient::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSendWebClient);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceiveWebClient);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(AppConfig::AsciiWebClient);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::DebugWebClient);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSendWebClient);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalWebClient)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerIP->setText(AppConfig::WebServerIP);
|
||||
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerPort->setText(QString::number(AppConfig::WebServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmWebClient::saveConfig()
|
||||
{
|
||||
AppConfig::HexSendWebClient = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceiveWebClient = ui->ckHexReceive->isChecked();
|
||||
AppConfig::AsciiWebClient = ui->ckAscii->isChecked();
|
||||
AppConfig::DebugWebClient = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoSendWebClient = ui->ckAutoSend->isChecked();
|
||||
AppConfig::IntervalWebClient = ui->cboxInterval->currentText().toInt();
|
||||
AppConfig::WebServerIP = ui->txtServerIP->text().trimmed();
|
||||
AppConfig::WebServerPort = ui->txtServerPort->text().trimmed().toInt();
|
||||
AppConfig::writeConfig();
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmWebClient::initTimer()
|
||||
{
|
||||
if (timer->interval() != AppConfig::IntervalWebClient) {
|
||||
timer->setInterval(AppConfig::IntervalWebClient);
|
||||
}
|
||||
|
||||
if (AppConfig::AutoSendWebClient) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmWebClient::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("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmWebClient::connected()
|
||||
{
|
||||
isOk = true;
|
||||
ui->btnConnect->setText("断开");
|
||||
append(0, "服务器连接");
|
||||
append(0, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort()));
|
||||
append(0, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
|
||||
}
|
||||
|
||||
void frmWebClient::disconnected()
|
||||
{
|
||||
isOk = false;
|
||||
ui->btnConnect->setText("连接");
|
||||
append(1, "服务器断开");
|
||||
}
|
||||
|
||||
void frmWebClient::error()
|
||||
{
|
||||
append(2, socket->errorString());
|
||||
}
|
||||
|
||||
void frmWebClient::sendData(const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (AppConfig::HexSendWebClient) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toUtf8();
|
||||
}
|
||||
|
||||
if (AppConfig::AsciiWebClient) {
|
||||
socket->sendTextMessage(data);
|
||||
} else {
|
||||
socket->sendBinaryMessage(buffer);
|
||||
}
|
||||
|
||||
append(0, data);
|
||||
}
|
||||
|
||||
void frmWebClient::textFrameReceived(const QString &data, bool isLastFrame)
|
||||
{
|
||||
QString buffer = data;
|
||||
append(1, buffer);
|
||||
|
||||
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
|
||||
if (AppConfig::DebugWebClient) {
|
||||
int count = AppData::Keys.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (AppData::Keys.at(i) == buffer) {
|
||||
sendData(AppData::Values.at(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmWebClient::binaryFrameReceived(const QByteArray &data, bool isLastFrame)
|
||||
{
|
||||
QString buffer;
|
||||
if (AppConfig::HexReceiveWebClient) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else {
|
||||
buffer = QString(data);
|
||||
}
|
||||
|
||||
textFrameReceived(buffer, isLastFrame);
|
||||
}
|
||||
|
||||
void frmWebClient::textMessageReceived(const QString &data)
|
||||
{
|
||||
textFrameReceived(data, true);
|
||||
}
|
||||
|
||||
void frmWebClient::binaryMessageReceived(const QByteArray &data)
|
||||
{
|
||||
binaryFrameReceived(data, true);
|
||||
}
|
||||
|
||||
void frmWebClient::on_btnConnect_clicked()
|
||||
{
|
||||
if (ui->btnConnect->text() == "连接") {
|
||||
QString url = QString("%1:%2").arg(AppConfig::WebServerIP).arg(AppConfig::WebServerPort);
|
||||
socket->abort();
|
||||
socket->open(QUrl(url));
|
||||
} else {
|
||||
socket->abort();
|
||||
}
|
||||
}
|
||||
|
||||
void frmWebClient::on_btnSave_clicked()
|
||||
{
|
||||
QString data = ui->txtMain->toPlainText();
|
||||
AppData::saveData(data);
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmWebClient::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmWebClient::on_btnSend_clicked()
|
||||
{
|
||||
if (!isOk) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString data = ui->cboxData->currentText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendData(data);
|
||||
}
|
||||
54
tool/nettool/form/frmwebclient.h
Normal file
54
tool/nettool/form/frmwebclient.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef FRMWEBCLIENT_H
|
||||
#define FRMWEBCLIENT_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtWebSockets>
|
||||
|
||||
namespace Ui {
|
||||
class frmWebClient;
|
||||
}
|
||||
|
||||
class frmWebClient : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmWebClient(QWidget *parent = 0);
|
||||
~frmWebClient();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
Ui::frmWebClient *ui;
|
||||
|
||||
bool isOk;
|
||||
QWebSocket *socket;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void initTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void connected();
|
||||
void disconnected();
|
||||
void error();
|
||||
void sendData(const QString &data);
|
||||
|
||||
void textFrameReceived(const QString &data, bool isLastFrame);
|
||||
void binaryFrameReceived(const QByteArray &data, bool isLastFrame);
|
||||
void textMessageReceived(const QString &data);
|
||||
void binaryMessageReceived(const QByteArray &data);
|
||||
|
||||
private slots:
|
||||
void on_btnConnect_clicked();
|
||||
void on_btnSave_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnSend_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMWEBCLIENT_H
|
||||
225
tool/nettool/form/frmwebclient.ui
Normal file
225
tool/nettool/form/frmwebclient.ui
Normal file
@@ -0,0 +1,225 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmWebClient</class>
|
||||
<widget class="QWidget" name="frmWebClient">
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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_5">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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>文本字符</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="btnConnect">
|
||||
<property name="text">
|
||||
<string>连接</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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="spacer">
|
||||
<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="layTcpClient">
|
||||
<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>
|
||||
268
tool/nettool/form/frmwebserver.cpp
Normal file
268
tool/nettool/form/frmwebserver.cpp
Normal file
@@ -0,0 +1,268 @@
|
||||
#include "frmwebserver.h"
|
||||
#include "ui_frmwebserver.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
frmWebServer::frmWebServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmWebServer)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
on_btnListen_clicked();
|
||||
}
|
||||
|
||||
frmWebServer::~frmWebServer()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmWebServer::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmWebServer::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
isOk = false;
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
server = new WebServer("WebServer", QWebSocketServer::NonSecureMode, this);
|
||||
connect(server, SIGNAL(connected(QString, int)), this, SLOT(connected(QString, int)));
|
||||
connect(server, SIGNAL(disconnected(QString, int)), this, SLOT(disconnected(QString, int)));
|
||||
connect(server, SIGNAL(error(QString, int, QString)), this, SLOT(error(QString, int, QString)));
|
||||
connect(server, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));
|
||||
connect(server, SIGNAL(receiveData(QString, int, QString)), this, SLOT(receiveData(QString, int, QString)));
|
||||
|
||||
//定时器发送数据
|
||||
timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
||||
|
||||
//填充数据到下拉框
|
||||
ui->cboxInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
AppData::loadIP(ui->cboxListenIP);
|
||||
}
|
||||
|
||||
void frmWebServer::initConfig()
|
||||
{
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSendWebServer);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceiveWebServer);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAscii->setChecked(AppConfig::AsciiWebServer);
|
||||
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::DebugWebServer);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSendWebServer);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalWebServer)));
|
||||
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(AppConfig::WebListenIP));
|
||||
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort->setText(QString::number(AppConfig::WebListenPort));
|
||||
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckSelectAll->setChecked(AppConfig::SelectAllWebServer);
|
||||
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmWebServer::saveConfig()
|
||||
{
|
||||
AppConfig::HexSendWebServer = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceiveWebServer = ui->ckHexReceive->isChecked();
|
||||
AppConfig::AsciiWebServer = ui->ckAscii->isChecked();
|
||||
AppConfig::DebugWebServer = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoSendWebServer = ui->ckAutoSend->isChecked();
|
||||
AppConfig::IntervalWebServer = ui->cboxInterval->currentText().toInt();
|
||||
AppConfig::WebListenIP = ui->cboxListenIP->currentText();
|
||||
AppConfig::WebListenPort = ui->txtListenPort->text().trimmed().toInt();
|
||||
AppConfig::SelectAllWebServer = ui->ckSelectAll->isChecked();
|
||||
AppConfig::writeConfig();
|
||||
|
||||
this->initTimer();
|
||||
}
|
||||
|
||||
void frmWebServer::initTimer()
|
||||
{
|
||||
if (timer->interval() != AppConfig::IntervalWebServer) {
|
||||
timer->setInterval(AppConfig::IntervalWebServer);
|
||||
}
|
||||
|
||||
if (AppConfig::AutoSendWebServer) {
|
||||
if (!timer->isActive()) {
|
||||
timer->start();
|
||||
}
|
||||
} else {
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmWebServer::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("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmWebServer::connected(const QString &ip, int port)
|
||||
{
|
||||
append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端上线"));
|
||||
|
||||
QString str = QString("%1:%2").arg(ip).arg(port);
|
||||
ui->listWidget->addItem(str);
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmWebServer::disconnected(const QString &ip, int port)
|
||||
{
|
||||
append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端下线"));
|
||||
|
||||
int row = -1;
|
||||
QString str = QString("%1:%2").arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget->count(); i++) {
|
||||
if (ui->listWidget->item(i)->text() == str) {
|
||||
row = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete ui->listWidget->takeItem(row);
|
||||
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
|
||||
}
|
||||
|
||||
void frmWebServer::error(const QString &ip, int port, const QString &error)
|
||||
{
|
||||
append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg(error));
|
||||
}
|
||||
|
||||
void frmWebServer::sendData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg(data));
|
||||
}
|
||||
|
||||
void frmWebServer::receiveData(const QString &ip, int port, const QString &data)
|
||||
{
|
||||
append(1, QString("[%1:%2] %3").arg(ip).arg(port).arg(data));
|
||||
}
|
||||
|
||||
void frmWebServer::on_btnListen_clicked()
|
||||
{
|
||||
if (ui->btnListen->text() == "监听") {
|
||||
isOk = server->start();
|
||||
if (isOk) {
|
||||
append(0, "监听成功");
|
||||
ui->btnListen->setText("关闭");
|
||||
}
|
||||
} else {
|
||||
isOk = false;
|
||||
server->stop();
|
||||
ui->btnListen->setText("监听");
|
||||
}
|
||||
}
|
||||
|
||||
void frmWebServer::on_btnSave_clicked()
|
||||
{
|
||||
QString data = ui->txtMain->toPlainText();
|
||||
AppData::saveData(data);
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmWebServer::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmWebServer::on_btnSend_clicked()
|
||||
{
|
||||
if (!isOk) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString data = ui->cboxData->currentText();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
server->writeData(data);
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
server->writeData(list.at(0), list.at(1).toInt(), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmWebServer::on_btnRemove_clicked()
|
||||
{
|
||||
if (ui->ckSelectAll->isChecked()) {
|
||||
server->remove();
|
||||
} else {
|
||||
int row = ui->listWidget->currentRow();
|
||||
if (row >= 0) {
|
||||
QString str = ui->listWidget->item(row)->text();
|
||||
QStringList list = str.split(":");
|
||||
server->remove(list.at(0), list.at(1).toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
52
tool/nettool/form/frmwebserver.h
Normal file
52
tool/nettool/form/frmwebserver.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef FRMWEBSERVER_H
|
||||
#define FRMWEBSERVER_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "webserver.h"
|
||||
|
||||
namespace Ui {
|
||||
class frmWebServer;
|
||||
}
|
||||
|
||||
class frmWebServer : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmWebServer(QWidget *parent = 0);
|
||||
~frmWebServer();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
Ui::frmWebServer *ui;
|
||||
|
||||
bool isOk;
|
||||
WebServer *server;
|
||||
QTimer *timer;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void initTimer();
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void connected(const QString &ip, int port);
|
||||
void disconnected(const QString &ip, int port);
|
||||
void error(const QString &ip, int port, const QString &error);
|
||||
|
||||
void sendData(const QString &ip, int port, const QString &data);
|
||||
void receiveData(const QString &ip, int port, const QString &data);
|
||||
|
||||
private slots:
|
||||
void on_btnListen_clicked();
|
||||
void on_btnSave_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnSend_clicked();
|
||||
void on_btnRemove_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMWEBSERVER_H
|
||||
271
tool/nettool/form/frmwebserver.ui
Normal file
271
tool/nettool/form/frmwebserver.ui
Normal file
@@ -0,0 +1,271 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmWebServer</class>
|
||||
<widget class="QWidget" name="frmWebServer">
|
||||
<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">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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_3">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<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>文本字符</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="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">
|
||||
<string>监听端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtListenPort"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnListen">
|
||||
<property name="text">
|
||||
<string>监听</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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>
|
||||
<widget class="QPushButton" name="btnRemove">
|
||||
<property name="text">
|
||||
<string>移除</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labCount">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>25</height>
|
||||
</size>
|
||||
</property>
|
||||
<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>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<layout class="QHBoxLayout" name="layTcpServer">
|
||||
<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>
|
||||
<tabstops>
|
||||
<tabstop>txtMain</tabstop>
|
||||
<tabstop>cboxData</tabstop>
|
||||
<tabstop>btnSend</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>btnRemove</tabstop>
|
||||
<tabstop>listWidget</tabstop>
|
||||
<tabstop>ckSelectAll</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user