qt_demoe/nettool/form/frmudpserver.cpp

272 lines
7.6 KiB
C++
Raw Normal View History

2019-09-29 05:13:01 +00:00
#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();
2019-12-05 08:01:43 +00:00
this->initIP();
2019-09-29 05:13:01 +00:00
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);
2019-12-05 08:01:43 +00:00
QUIHelper::setLabStyle(ui->labCount, 3);
}
void frmUdpServer::initIP()
{
//获取本机所有IP
2021-01-22 09:20:01 +00:00
QStringList ips = QUIHelper::getLocalIPs();
2019-12-05 08:01:43 +00:00
ui->cboxListenIP->addItems(ips);
ui->cboxListenIP->addItem("127.0.0.1");
2019-09-29 05:13:01 +00:00
}
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()));
2019-12-05 08:01:43 +00:00
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(App::UdpListenIP));
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
2019-09-29 05:13:01 +00:00
ui->txtListenPort->setText(QString::number(App::UdpListenPort));
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
2019-12-05 08:01:43 +00:00
ui->ckSelectAll->setChecked(App::SelectAllUdpServer);
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
this->changeTimer();
2019-09-29 05:13:01 +00:00
}
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();
2019-12-05 08:01:43 +00:00
App::UdpListenIP = ui->cboxListenIP->currentText();
2019-09-29 05:13:01 +00:00
App::UdpListenPort = ui->txtListenPort->text().trimmed().toInt();
2019-12-05 08:01:43 +00:00
App::SelectAllUdpServer = ui->ckSelectAll->isChecked();
2019-09-29 05:13:01 +00:00
App::writeConfig();
2019-12-05 08:01:43 +00:00
this->changeTimer();
}
void frmUdpServer::changeTimer()
{
2019-09-29 05:13:01 +00:00
timer->setInterval(App::IntervalUdpServer);
2019-12-05 08:01:43 +00:00
if (App::AutoSendUdpServer) {
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 = "发送";
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();
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);
2019-12-05 08:01:43 +00:00
clientConnected(ip, port);
2019-09-29 05:13:01 +00:00
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);
2019-12-05 08:01:43 +00:00
}
2019-09-29 05:13:01 +00:00
2019-12-05 08:01:43 +00:00
void frmUdpServer::clientConnected(const QString &ip, int port)
{
//先过滤重复的
QString str = QString("%1:%2").arg(ip).arg(port);
for (int i = 0; i < ui->listWidget->count(); i++) {
QString s = ui->listWidget->item(i)->text();
if (str == s) {
return;
}
}
ui->listWidget->addItem(str);
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
2019-09-29 05:13:01 +00:00
}
void frmUdpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
2019-12-05 08:01:43 +00:00
bool ok = udpSocket->bind(QHostAddress(App::UdpListenIP), App::UdpListenPort);
2019-09-29 05:13:01 +00:00
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;
}
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
}