qt_demoe/tool/nettool/form/frmtcpclient.cpp

283 lines
8.1 KiB
C++
Raw Normal View History

2019-09-29 05:13:01 +00:00
#include "frmtcpclient.h"
#include "ui_frmtcpclient.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
frmTcpClient::frmTcpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcpClient)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
2019-09-29 05:13:01 +00:00
}
frmTcpClient::~frmTcpClient()
{
delete ui;
}
2021-08-22 02:57:02 +00:00
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);
}
2019-09-29 05:13:01 +00:00
void frmTcpClient::initForm()
{
2021-08-22 02:57:02 +00:00
QFont font;
font.setPixelSize(16);
ui->txtMain->setFont(font);
ui->txtMain->viewport()->installEventFilter(this);
2019-09-29 05:13:01 +00:00
isOk = false;
//实例化对象并绑定信号槽
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
2021-08-22 02:57:02 +00:00
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
2021-08-22 02:57:02 +00:00
connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error()));
#else
2021-08-22 02:57:02 +00:00
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
#endif
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
2019-09-29 05:13:01 +00:00
//定时器发送数据
2019-09-29 05:13:01 +00:00
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
//填充数据到下拉框
ui->cboxInterval->addItems(AppData::Intervals);
ui->cboxData->addItems(AppData::Datas);
2021-11-17 07:33:19 +00:00
AppData::loadIP(ui->cboxBindIP);
2019-09-29 05:13:01 +00:00
}
void frmTcpClient::initConfig()
{
2021-04-13 01:36:22 +00:00
ui->ckHexSend->setChecked(AppConfig::HexSendTcpClient);
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::HexReceiveTcpClient);
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::AsciiTcpClient);
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::DebugTcpClient);
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::AutoSendTcpClient);
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::IntervalTcpClient)));
2019-09-29 05:13:01 +00:00
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()));
2021-04-13 01:36:22 +00:00
ui->txtServerIP->setText(AppConfig::TcpServerIP);
2019-09-29 05:13:01 +00:00
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->txtServerPort->setText(QString::number(AppConfig::TcpServerPort));
2019-09-29 05:13:01 +00:00
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
this->initTimer();
2019-09-29 05:13:01 +00:00
}
void frmTcpClient::saveConfig()
{
2021-04-13 01:36:22 +00:00
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();
2021-04-13 01:36:22 +00:00
AppConfig::TcpServerIP = ui->txtServerIP->text().trimmed();
AppConfig::TcpServerPort = ui->txtServerPort->text().trimmed().toInt();
AppConfig::writeConfig();
2019-09-29 05:13:01 +00:00
this->initTimer();
2019-12-05 08:01:43 +00:00
}
void frmTcpClient::initTimer()
2019-12-05 08:01:43 +00:00
{
if (timer->interval() != AppConfig::IntervalTcpClient) {
timer->setInterval(AppConfig::IntervalTcpClient);
}
2021-04-13 01:36:22 +00:00
if (AppConfig::AutoSendTcpClient) {
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 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 = "发送";
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++;
}
void frmTcpClient::connected()
{
isOk = true;
ui->btnConnect->setText("断开");
append(0, "服务器连接");
2021-08-22 02:57:02 +00:00
append(0, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort()));
append(0, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
2019-09-29 05:13:01 +00:00
}
void frmTcpClient::disconnected()
{
isOk = false;
ui->btnConnect->setText("连接");
append(1, "服务器断开");
2021-08-22 02:57:02 +00:00
}
void frmTcpClient::error()
{
append(2, socket->errorString());
2019-09-29 05:13:01 +00:00
}
void frmTcpClient::readData()
{
QByteArray data = socket->readAll();
2019-09-29 05:13:01 +00:00
if (data.length() <= 0) {
return;
}
QString buffer;
2021-04-13 01:36:22 +00:00
if (AppConfig::HexReceiveTcpClient) {
2021-11-17 07:33:19 +00:00
buffer = QUIHelperData::byteArrayToHexStr(data);
2021-04-13 01:36:22 +00:00
} else if (AppConfig::AsciiTcpClient) {
2021-11-17 07:33:19 +00:00
buffer = QUIHelperData::byteArrayToAsciiStr(data);
2019-09-29 05:13:01 +00:00
} else {
buffer = QString(data);
}
append(1, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
2021-04-13 01:36:22 +00:00
if (AppConfig::DebugTcpClient) {
int count = AppData::Keys.count();
2019-09-29 05:13:01 +00:00
for (int i = 0; i < count; i++) {
if (AppData::Keys.at(i) == buffer) {
sendData(AppData::Values.at(i));
2019-09-29 05:13:01 +00:00
break;
}
}
}
}
void frmTcpClient::sendData(const QString &data)
{
QByteArray buffer;
2021-04-13 01:36:22 +00:00
if (AppConfig::HexSendTcpClient) {
2021-11-17 07:33:19 +00:00
buffer = QUIHelperData::hexStrToByteArray(data);
2021-04-13 01:36:22 +00:00
} else if (AppConfig::AsciiTcpClient) {
2021-11-17 07:33:19 +00:00
buffer = QUIHelperData::asciiStrToByteArray(data);
2019-09-29 05:13:01 +00:00
} else {
buffer = data.toUtf8();
2019-09-29 05:13:01 +00:00
}
socket->write(buffer);
2019-09-29 05:13:01 +00:00
append(0, data);
}
void frmTcpClient::on_btnConnect_clicked()
{
if (ui->btnConnect->text() == "连接") {
//断开所有连接和操作
2021-08-22 02:57:02 +00:00
//socket->abort();
//绑定网卡和端口
2021-08-22 02:57:02 +00:00
//有个后遗症,客户端这边断开连接后还会保持几分钟导致不能重复绑定
//如果是服务器断开则可以继续使用
2021-08-21 08:10:22 +00:00
//提示 The bound address is already in use
//参考 https://www.cnblogs.com/baiduboy/p/7426822.html
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
2021-08-21 08:10:22 +00:00
//socket->bind(QHostAddress(AppConfig::TcpBindIP), AppConfig::TcpBindPort);
#endif
//连接服务器
2021-04-13 01:36:22 +00:00
socket->connectToHost(AppConfig::TcpServerIP, AppConfig::TcpServerPort);
2019-09-29 05:13:01 +00:00
} else {
socket->abort();
2019-09-29 05:13:01 +00:00
}
}
void frmTcpClient::on_btnSave_clicked()
{
QString data = ui->txtMain->toPlainText();
AppData::saveData(data);
2019-09-29 05:13:01 +00:00
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);
}