首次提交

This commit is contained in:
feiyangqingyun
2019-10-04 18:37:23 +08:00
commit 315f102884
208 changed files with 26451 additions and 0 deletions

17
nettool/form/form.pri Normal file
View File

@@ -0,0 +1,17 @@
FORMS += \
$$PWD/frmmain.ui \
$$PWD/frmtcpclient.ui \
$$PWD/frmtcpserver.ui \
$$PWD/frmudpserver.ui
HEADERS += \
$$PWD/frmmain.h \
$$PWD/frmtcpclient.h \
$$PWD/frmtcpserver.h \
$$PWD/frmudpserver.h
SOURCES += \
$$PWD/frmmain.cpp \
$$PWD/frmtcpclient.cpp \
$$PWD/frmtcpserver.cpp \
$$PWD/frmudpserver.cpp

20
nettool/form/frmmain.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "frmmain.h"
#include "ui_frmmain.h"
#include "quiwidget.h"
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
{
ui->setupUi(this);
ui->tabWidget->setCurrentIndex(App::CurrentIndex);
}
frmMain::~frmMain()
{
delete ui;
}
void frmMain::on_tabWidget_currentChanged(int index)
{
App::CurrentIndex = index;
App::writeConfig();
}

25
nettool/form/frmmain.h Normal file
View File

@@ -0,0 +1,25 @@
#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 slots:
void on_tabWidget_currentChanged(int index);
private:
Ui::frmMain *ui;
};
#endif // FRMMAIN_H

81
nettool/form/frmmain.ui Normal file
View File

@@ -0,0 +1,81 @@
<?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>2</number>
</property>
<widget class="frmTcpClient" name="tabTcpClient">
<attribute name="title">
<string>TCP客户端</string>
</attribute>
</widget>
<widget class="frmTcpServer" name="tabTcpServer">
<attribute name="title">
<string>TCP服务器</string>
</attribute>
</widget>
<widget class="frmUdpServer" name="tabUdpServer">
<attribute name="title">
<string>UDP服务器</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>frmTcpClient</class>
<extends>QWidget</extends>
<header>frmtcpclient.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>frmTcpServer</class>
<extends>QWidget</extends>
<header>frmtcpserver.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>frmUdpServer</class>
<extends>QWidget</extends>
<header>frmudpserver.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,223 @@
#include "frmtcpclient.h"
#include "ui_frmtcpclient.h"
#include "quiwidget.h"
frmTcpClient::frmTcpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcpClient)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmTcpClient::~frmTcpClient()
{
delete ui;
}
void frmTcpClient::initForm()
{
isOk = false;
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
ui->cboxInterval->addItems(App::Intervals);
ui->cboxData->addItems(App::Datas);
}
void frmTcpClient::initConfig()
{
ui->ckHexSend->setChecked(App::HexSendTcpClient);
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckHexReceive->setChecked(App::HexReceiveTcpClient);
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAscii->setChecked(App::AsciiTcpClient);
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckDebug->setChecked(App::DebugTcpClient);
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAutoSend->setChecked(App::AutoSendTcpClient);
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalTcpClient)));
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtServerIP->setText(App::TcpServerIP);
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtServerPort->setText(QString::number(App::TcpServerPort));
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
timer->setInterval(App::IntervalTcpClient);
App::AutoSendTcpClient ? timer->start() : timer->stop();
}
void frmTcpClient::saveConfig()
{
App::HexSendTcpClient = ui->ckHexSend->isChecked();
App::HexReceiveTcpClient = ui->ckHexReceive->isChecked();
App::AsciiTcpClient = ui->ckAscii->isChecked();
App::DebugTcpClient = ui->ckDebug->isChecked();
App::AutoSendTcpClient = ui->ckAutoSend->isChecked();
App::IntervalTcpClient = ui->cboxInterval->currentText().toInt();
App::TcpServerIP = ui->txtServerIP->text().trimmed();
App::TcpServerPort = ui->txtServerPort->text().trimmed().toInt();
App::writeConfig();
timer->setInterval(App::IntervalTcpClient);
App::AutoSendTcpClient ? timer->start() : 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("darkgreen"));
} else {
strType = "接收";
ui->txtMain->setTextColor(QColor("red"));
}
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
ui->txtMain->append(strData);
currentCount++;
}
void frmTcpClient::connected()
{
isOk = true;
ui->btnConnect->setText("断开");
append(0, "服务器连接");
}
void frmTcpClient::disconnected()
{
isOk = false;
tcpSocket->abort();
ui->btnConnect->setText("连接");
append(1, "服务器断开");
}
void frmTcpClient::readData()
{
QByteArray data = tcpSocket->readAll();
if (data.length() <= 0) {
return;
}
QString buffer;
if (App::HexReceiveTcpClient) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else if (App::AsciiTcpClient) {
buffer = QUIHelper::byteArrayToAsciiStr(data);
} else {
buffer = QString(data);
}
append(1, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
if (App::DebugTcpClient) {
int count = App::Keys.count();
for (int i = 0; i < count; i++) {
if (App::Keys.at(i) == buffer) {
sendData(App::Values.at(i));
break;
}
}
}
}
void frmTcpClient::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexSendTcpClient) {
buffer = QUIHelper::hexStrToByteArray(data);
} else if (App::AsciiTcpClient) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toLatin1();
}
tcpSocket->write(buffer);
append(0, data);
}
void frmTcpClient::on_btnConnect_clicked()
{
if (ui->btnConnect->text() == "连接") {
tcpSocket->abort();
tcpSocket->connectToHost(App::TcpServerIP, App::TcpServerPort);
} else {
tcpSocket->abort();
}
}
void frmTcpClient::on_btnSave_clicked()
{
QString data = ui->txtMain->toPlainText();
if (data.length() <= 0) {
return;
}
QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(STRDATETIME);
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Text)) {
file.write(data.toUtf8());
file.close();
}
on_btnClear_clicked();
}
void 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);
}

View File

@@ -0,0 +1,44 @@
#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();
private:
Ui::frmTcpClient *ui;
bool isOk;
QTcpSocket *tcpSocket;
QTimer *timer;
private slots:
void initForm();
void initConfig();
void saveConfig();
void append(int type, const QString &data, bool clear = false);
void connected();
void disconnected();
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

View File

@@ -0,0 +1,213 @@
<?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">
<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>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</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>Ascii控制字符</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckShow">
<property name="text">
<string>暂停显示</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckDebug">
<property name="text">
<string>模拟设备</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckAutoSend">
<property name="text">
<string>定时发送</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cboxInterval"/>
</item>
<item>
<widget class="QLabel" name="labServerIP">
<property name="text">
<string>服务器IP地址</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>

View File

@@ -0,0 +1,232 @@
#include "frmtcpserver.h"
#include "ui_frmtcpserver.h"
#include "quiwidget.h"
frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcpServer)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmTcpServer::~frmTcpServer()
{
delete ui;
}
void frmTcpServer::initForm()
{
isOk = false;
tcpServer = new TcpServer(this);
connect(tcpServer, SIGNAL(clientConnected(QString, int)), this, SLOT(clientConnected(QString, int)));
connect(tcpServer, SIGNAL(clientDisconnected(QString, int)), this, SLOT(clientDisconnected(QString, int)));
connect(tcpServer, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));
connect(tcpServer, 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(App::Intervals);
ui->cboxData->addItems(App::Datas);
QUIHelper::setLabStyle(ui->labCount, 3);
}
void frmTcpServer::initConfig()
{
ui->ckHexSend->setChecked(App::HexSendTcpServer);
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckHexReceive->setChecked(App::HexReceiveTcpServer);
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAscii->setChecked(App::AsciiTcpServer);
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckDebug->setChecked(App::DebugTcpServer);
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAutoSend->setChecked(App::AutoSendTcpServer);
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckSelectAll->setChecked(App::SelectAllTcpServer);
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalTcpServer)));
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtListenPort->setText(QString::number(App::TcpListenPort));
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
timer->setInterval(App::IntervalTcpServer);
App::AutoSendTcpServer ? timer->start() : timer->stop();
}
void frmTcpServer::saveConfig()
{
App::HexSendTcpServer = ui->ckHexSend->isChecked();
App::HexReceiveTcpServer = ui->ckHexReceive->isChecked();
App::AsciiTcpServer = ui->ckAscii->isChecked();
App::DebugTcpServer = ui->ckDebug->isChecked();
App::AutoSendTcpServer = ui->ckAutoSend->isChecked();
App::SelectAllTcpServer = ui->ckSelectAll->isChecked();
App::IntervalTcpServer = ui->cboxInterval->currentText().toInt();
App::TcpListenPort = ui->txtListenPort->text().trimmed().toInt();
App::writeConfig();
timer->setInterval(App::IntervalTcpServer);
App::AutoSendTcpServer ? timer->start() : 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("darkgreen"));
} else {
strType = "接收";
ui->txtMain->setTextColor(QColor("red"));
}
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
ui->txtMain->append(strData);
currentCount++;
}
void frmTcpServer::sendData(const QString &data)
{
if (ui->ckSelectAll->isChecked()) {
tcpServer->writeData(data);
} else {
int row = ui->listWidget->currentRow();
if (row >= 0) {
QString str = ui->listWidget->item(row)->text();
QStringList list = str.split(":");
tcpServer->writeData(list.at(0), list.at(1).toInt(), data);
}
}
}
void frmTcpServer::clientConnected(const QString &ip, int port)
{
QString str = QString("%1:%2").arg(ip).arg(port);
ui->listWidget->addItem(str);
ui->labCount->setText(QString("共 %1 个连接").arg(ui->listWidget->count()));
}
void frmTcpServer::clientDisconnected(const QString &ip, int port)
{
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::sendData(const QString &ip, int port, const QString &data)
{
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
bool error = (data.contains("下线") || data.contains("离线"));
append(error ? 1 : 0, str);
}
void frmTcpServer::receiveData(const QString &ip, int port, const QString &data)
{
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
append(1, str);
}
void frmTcpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
isOk = tcpServer->start();
if (isOk) {
append(0, "监听成功");
ui->btnListen->setText("关闭");
}
} else {
isOk = false;
tcpServer->stop();
ui->btnListen->setText("监听");
}
}
void frmTcpServer::on_btnSave_clicked()
{
QString data = ui->txtMain->toPlainText();
if (data.length() <= 0) {
return;
}
QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(STRDATETIME);
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Text)) {
file.write(data.toUtf8());
file.close();
}
on_btnClear_clicked();
}
void 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;
}
sendData(data);
}
void frmTcpServer::on_btnClose_clicked()
{
if (ui->ckSelectAll->isChecked()) {
tcpServer->remove();
} else {
int row = ui->listWidget->currentRow();
if (row >= 0) {
QString str = ui->listWidget->item(row)->text();
QStringList list = str.split(":");
tcpServer->remove(list.at(0), list.at(1).toInt());
}
}
}

View File

@@ -0,0 +1,48 @@
#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();
private:
Ui::frmTcpServer *ui;
bool isOk;
TcpServer *tcpServer;
QTimer *timer;
private slots:
void initForm();
void initConfig();
void saveConfig();
void append(int type, const QString &data, bool clear = false);
private slots:
void sendData(const QString &data);
void clientConnected(const QString &ip, int port);
void clientDisconnected(const QString &ip, int port);
void sendData(const QString &ip, int port, const QString &data);
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_btnClose_clicked();
};
#endif // FRMTCPSERVER_H

View File

@@ -0,0 +1,229 @@
<?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">
<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>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</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>Ascii控制字符</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckShow">
<property name="text">
<string>暂停显示</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckDebug">
<property name="text">
<string>模拟设备</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckAutoSend">
<property name="text">
<string>定时发送</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cboxInterval"/>
</item>
<item>
<widget class="QLabel" name="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="btnClose">
<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>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,224 @@
#include "frmudpserver.h"
#include "ui_frmudpserver.h"
#include "quiwidget.h"
frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdpServer)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmUdpServer::~frmUdpServer()
{
delete ui;
}
void frmUdpServer::initForm()
{
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
ui->cboxInterval->addItems(App::Intervals);
ui->cboxData->addItems(App::Datas);
}
void frmUdpServer::initConfig()
{
ui->ckHexSend->setChecked(App::HexSendUdpServer);
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckHexReceive->setChecked(App::HexReceiveUdpServer);
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAscii->setChecked(App::AsciiUdpServer);
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckDebug->setChecked(App::DebugUdpServer);
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAutoSend->setChecked(App::AutoSendUdpServer);
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalUdpServer)));
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtServerIP->setText(App::UdpServerIP);
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtServerPort->setText(QString::number(App::UdpServerPort));
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtListenPort->setText(QString::number(App::UdpListenPort));
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
timer->setInterval(App::IntervalUdpServer);
App::AutoSendUdpServer ? timer->start() : timer->stop();
}
void frmUdpServer::saveConfig()
{
App::HexSendUdpServer = ui->ckHexSend->isChecked();
App::HexReceiveUdpServer = ui->ckHexReceive->isChecked();
App::AsciiUdpServer = ui->ckAscii->isChecked();
App::DebugUdpServer = ui->ckDebug->isChecked();
App::AutoSendUdpServer = ui->ckAutoSend->isChecked();
App::IntervalUdpServer = ui->cboxInterval->currentText().toInt();
App::UdpServerIP = ui->txtServerIP->text().trimmed();
App::UdpServerPort = ui->txtServerPort->text().trimmed().toInt();
App::UdpListenPort = ui->txtListenPort->text().trimmed().toInt();
App::writeConfig();
timer->setInterval(App::IntervalUdpServer);
App::AutoSendUdpServer ? timer->start() : 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("darkgreen"));
} else {
strType = "接收";
ui->txtMain->setTextColor(QColor("red"));
}
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
ui->txtMain->append(strData);
currentCount++;
}
void frmUdpServer::readData()
{
QHostAddress host;
quint16 port;
QByteArray data;
QString buffer;
while (udpSocket->hasPendingDatagrams()) {
data.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(data.data(), data.size(), &host, &port);
if (App::HexReceiveUdpServer) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else if (App::AsciiUdpServer) {
buffer = QUIHelper::byteArrayToAsciiStr(data);
} else {
buffer = QString(data);
}
QString ip = host.toString();
if (ip.isEmpty()) {
continue;
}
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer);
append(1, str);
if (App::DebugUdpServer) {
int count = App::Keys.count();
for (int i = 0; i < count; i++) {
if (App::Keys.at(i) == buffer) {
sendData(ip, port, App::Values.at(i));
break;
}
}
}
}
}
void frmUdpServer::sendData(const QString &ip, int port, const QString &data)
{
QByteArray buffer;
if (App::HexSendUdpServer) {
buffer = QUIHelper::hexStrToByteArray(data);
} else if (App::AsciiUdpServer) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toLatin1();
}
udpSocket->writeDatagram(buffer, QHostAddress(ip), port);
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
append(0, str);
}
void frmUdpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
bool ok = udpSocket->bind(QHostAddress::AnyIPv4, App::UdpListenPort);
#else
bool ok = udpSocket->bind(QHostAddress::Any, App::UdpListenPort);
#endif
if (ok) {
ui->btnListen->setText("关闭");
append(0, "监听成功");
}
} else {
udpSocket->abort();
ui->btnListen->setText("监听");
}
}
void frmUdpServer::on_btnSave_clicked()
{
QString data = ui->txtMain->toPlainText();
if (data.length() <= 0) {
return;
}
QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(STRDATETIME);
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Text)) {
file.write(data.toUtf8());
file.close();
}
on_btnClear_clicked();
}
void frmUdpServer::on_btnClear_clicked()
{
append(0, "", true);
}
void frmUdpServer::on_btnSend_clicked()
{
QString data = ui->cboxData->currentText();
if (data.length() <= 0) {
return;
}
sendData(App::UdpServerIP, App::UdpServerPort, data);
}

View File

@@ -0,0 +1,41 @@
#ifndef 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();
private:
Ui::frmUdpServer *ui;
QUdpSocket *udpSocket;
QTimer *timer;
private slots:
void initForm();
void initConfig();
void saveConfig();
void append(int type, const QString &data, bool clear = false);
void readData();
void sendData(const QString &ip, int port, const QString &data);
private slots:
void on_btnListen_clicked();
void on_btnSave_clicked();
void on_btnClear_clicked();
void on_btnSend_clicked();
};
#endif // FRMUDPSERVER_H

View File

@@ -0,0 +1,211 @@
<?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">
<item row="0" column="0">
<widget class="QTextEdit" name="txtMain">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<widget class="QFrame" name="frame">
<property name="minimumSize">
<size>
<width>170</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>170</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="ckHexReceive">
<property name="text">
<string>16进制接收</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckHexSend">
<property name="text">
<string>16进制发送</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckAscii">
<property name="text">
<string>Ascii控制字符</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckShow">
<property name="text">
<string>暂停显示</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckDebug">
<property name="text">
<string>模拟设备</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckAutoSend">
<property name="text">
<string>定时发送</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cboxInterval"/>
</item>
<item>
<widget class="QLabel" name="labServerIP">
<property name="text">
<string>远程IP地址</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="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>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="layUdpServer">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QComboBox" name="cboxData">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSend">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>80</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>发送</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>