2019-09-29 05:13:01 +00:00
|
|
|
|
#include "frmudpserver.h"
|
|
|
|
|
#include "ui_frmudpserver.h"
|
2021-06-08 11:02:49 +00:00
|
|
|
|
#include "quihelper.h"
|
2021-11-17 07:33:19 +00:00
|
|
|
|
#include "quihelperdata.h"
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
|
|
|
|
frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdpServer)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
this->initForm();
|
|
|
|
|
this->initConfig();
|
2021-08-21 08:10:22 +00:00
|
|
|
|
on_btnListen_clicked();
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frmUdpServer::~frmUdpServer()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 02:57:02 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 05:13:01 +00:00
|
|
|
|
void frmUdpServer::initForm()
|
|
|
|
|
{
|
2021-08-22 02:57:02 +00:00
|
|
|
|
QFont font;
|
|
|
|
|
font.setPixelSize(16);
|
|
|
|
|
ui->txtMain->setFont(font);
|
|
|
|
|
ui->txtMain->viewport()->installEventFilter(this);
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
//实例化对象并绑定信号槽
|
2021-01-24 08:04:48 +00:00
|
|
|
|
socket = new QUdpSocket(this);
|
2021-08-22 02:57:02 +00:00
|
|
|
|
#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
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
//定时器发送数据
|
2019-09-29 05:13:01 +00:00
|
|
|
|
timer = new QTimer(this);
|
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
//填充数据到下拉框
|
|
|
|
|
ui->cboxInterval->addItems(AppData::Intervals);
|
|
|
|
|
ui->cboxData->addItems(AppData::Datas);
|
|
|
|
|
AppData::loadIP(ui->cboxListenIP);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmUdpServer::initConfig()
|
|
|
|
|
{
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckHexSend->setChecked(AppConfig::HexSendUdpServer);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckHexReceive->setChecked(AppConfig::HexReceiveUdpServer);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckAscii->setChecked(AppConfig::AsciiUdpServer);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckDebug->setChecked(AppConfig::DebugUdpServer);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckAutoSend->setChecked(AppConfig::AutoSendUdpServer);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalUdpServer)));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(AppConfig::UdpListenIP));
|
2019-12-05 08:01:43 +00:00
|
|
|
|
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->txtListenPort->setText(QString::number(AppConfig::UdpListenPort));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckSelectAll->setChecked(AppConfig::SelectAllUdpServer);
|
2019-12-05 08:01:43 +00:00
|
|
|
|
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
this->initTimer();
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmUdpServer::saveConfig()
|
|
|
|
|
{
|
2021-04-13 01:36:22 +00:00
|
|
|
|
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();
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
this->initTimer();
|
2019-12-05 08:01:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
void frmUdpServer::initTimer()
|
2019-12-05 08:01:43 +00:00
|
|
|
|
{
|
2021-08-21 03:19:00 +00:00
|
|
|
|
if (timer->interval() != AppConfig::IntervalUdpServer) {
|
|
|
|
|
timer->setInterval(AppConfig::IntervalUdpServer);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::AutoSendUdpServer) {
|
2019-12-05 08:01:43 +00:00
|
|
|
|
if (!timer->isActive()) {
|
|
|
|
|
timer->start();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (timer->isActive()) {
|
|
|
|
|
timer->stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = "发送";
|
2021-08-21 08:10:22 +00:00
|
|
|
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
|
|
|
|
} else if (type == 1) {
|
2019-09-29 05:13:01 +00:00
|
|
|
|
strType = "接收";
|
2021-08-22 02:57:02 +00:00
|
|
|
|
ui->txtMain->setTextColor(QColor("#753775"));
|
2021-08-21 08:10:22 +00:00
|
|
|
|
} else {
|
2021-08-22 02:57:02 +00:00
|
|
|
|
strType = "错误";
|
|
|
|
|
ui->txtMain->setTextColor(QColor("#D64D54"));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
|
|
|
|
ui->txtMain->append(strData);
|
|
|
|
|
currentCount++;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 02:57:02 +00:00
|
|
|
|
void frmUdpServer::error()
|
|
|
|
|
{
|
|
|
|
|
append(2, socket->errorString());
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 05:13:01 +00:00
|
|
|
|
void frmUdpServer::readData()
|
|
|
|
|
{
|
|
|
|
|
QHostAddress host;
|
|
|
|
|
quint16 port;
|
|
|
|
|
QByteArray data;
|
|
|
|
|
QString buffer;
|
|
|
|
|
|
2021-01-24 08:04:48 +00:00
|
|
|
|
while (socket->hasPendingDatagrams()) {
|
|
|
|
|
data.resize(socket->pendingDatagramSize());
|
|
|
|
|
socket->readDatagram(data.data(), data.size(), &host, &port);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::HexReceiveUdpServer) {
|
2021-11-17 07:33:19 +00:00
|
|
|
|
buffer = QUIHelperData::byteArrayToHexStr(data);
|
2021-04-13 01:36:22 +00:00
|
|
|
|
} else if (AppConfig::AsciiUdpServer) {
|
2021-11-17 07:33:19 +00:00
|
|
|
|
buffer = QUIHelperData::byteArrayToAsciiStr(data);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
} else {
|
|
|
|
|
buffer = QString(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ip = host.toString();
|
2019-12-05 08:01:43 +00:00
|
|
|
|
ip = ip.replace("::ffff:", "");
|
2019-09-29 05:13:01 +00:00
|
|
|
|
if (ip.isEmpty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer);
|
|
|
|
|
append(1, str);
|
2021-08-22 02:57:02 +00:00
|
|
|
|
|
|
|
|
|
//先过滤重复的
|
|
|
|
|
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()));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::DebugUdpServer) {
|
2021-08-21 03:19:00 +00:00
|
|
|
|
int count = AppData::Keys.count();
|
2019-09-29 05:13:01 +00:00
|
|
|
|
for (int i = 0; i < count; i++) {
|
2021-08-21 03:19:00 +00:00
|
|
|
|
if (AppData::Keys.at(i) == buffer) {
|
|
|
|
|
sendData(ip, port, AppData::Values.at(i));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmUdpServer::sendData(const QString &ip, int port, const QString &data)
|
|
|
|
|
{
|
|
|
|
|
QByteArray buffer;
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::HexSendUdpServer) {
|
2021-11-17 07:33:19 +00:00
|
|
|
|
buffer = QUIHelperData::hexStrToByteArray(data);
|
2021-04-13 01:36:22 +00:00
|
|
|
|
} else if (AppConfig::AsciiUdpServer) {
|
2021-11-17 07:33:19 +00:00
|
|
|
|
buffer = QUIHelperData::asciiStrToByteArray(data);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
} else {
|
2021-01-24 08:04:48 +00:00
|
|
|
|
buffer = data.toUtf8();
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 08:04:48 +00:00
|
|
|
|
socket->writeDatagram(buffer, QHostAddress(ip), port);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
|
|
|
|
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
|
|
|
|
|
append(0, str);
|
2019-12-05 08:01:43 +00:00
|
|
|
|
}
|
2019-09-29 05:13:01 +00:00
|
|
|
|
|
|
|
|
|
void frmUdpServer::on_btnListen_clicked()
|
|
|
|
|
{
|
|
|
|
|
if (ui->btnListen->text() == "监听") {
|
2021-04-13 01:36:22 +00:00
|
|
|
|
bool ok = socket->bind(QHostAddress(AppConfig::UdpListenIP), AppConfig::UdpListenPort);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
if (ok) {
|
|
|
|
|
append(0, "监听成功");
|
2022-01-13 03:27:55 +00:00
|
|
|
|
ui->btnListen->setText("关闭");
|
|
|
|
|
} else {
|
|
|
|
|
append(2, QString("监听失败: %1").arg(socket->errorString()));
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-01-24 08:04:48 +00:00
|
|
|
|
socket->abort();
|
2019-09-29 05:13:01 +00:00
|
|
|
|
ui->btnListen->setText("监听");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmUdpServer::on_btnSave_clicked()
|
|
|
|
|
{
|
|
|
|
|
QString data = ui->txtMain->toPlainText();
|
2021-08-21 03:19:00 +00:00
|
|
|
|
AppData::saveData(data);
|
2019-09-29 05:13:01 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 08:01:43 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-29 05:13:01 +00:00
|
|
|
|
}
|
2021-08-22 02:57:02 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|