新增websocket调试助手集成在nettool中

This commit is contained in:
feiyangqingyun
2021-01-24 16:05:06 +08:00
parent 4b5b38c8eb
commit 18e8325bcf
46 changed files with 1941 additions and 419 deletions

View File

@@ -8,7 +8,7 @@ QString App::DeviceFileName = "device.txt";
QString App::PortName = "COM1";
int App::BaudRate = 9600;
int App::DataBit = 8;
QString App::Parity = "";
QString App::Parity = QString::fromUtf8("");
double App::StopBit = 1;
bool App::HexSend = false;
@@ -37,30 +37,30 @@ void App::readConfig()
QSettings set(App::ConfigFile, QSettings::IniFormat);
set.beginGroup("ComConfig");
App::PortName = set.value("PortName").toString();
App::BaudRate = set.value("BaudRate").toInt();
App::DataBit = set.value("DataBit").toInt();
App::Parity = set.value("Parity").toString();
App::StopBit = set.value("StopBit").toInt();
App::PortName = set.value("PortName", App::PortName).toString();
App::BaudRate = set.value("BaudRate", App::BaudRate).toInt();
App::DataBit = set.value("DataBit", App::DataBit).toInt();
App::Parity = set.value("Parity", App::Parity).toString();
App::StopBit = set.value("StopBit", App::StopBit).toInt();
App::HexSend = set.value("HexSend").toBool();
App::HexReceive = set.value("HexReceive").toBool();
App::Debug = set.value("Debug").toBool();
App::AutoClear = set.value("AutoClear").toBool();
App::HexSend = set.value("HexSend", App::HexSend).toBool();
App::HexReceive = set.value("HexReceive", App::HexReceive).toBool();
App::Debug = set.value("Debug", App::Debug).toBool();
App::AutoClear = set.value("AutoClear", App::AutoClear).toBool();
App::AutoSend = set.value("AutoSend").toBool();
App::SendInterval = set.value("SendInterval").toInt();
App::AutoSave = set.value("AutoSave").toBool();
App::SaveInterval = set.value("SaveInterval").toInt();
App::AutoSend = set.value("AutoSend", App::AutoSend).toBool();
App::SendInterval = set.value("SendInterval", App::SendInterval).toInt();
App::AutoSave = set.value("AutoSave", App::AutoSave).toBool();
App::SaveInterval = set.value("SaveInterval", App::SaveInterval).toInt();
set.endGroup();
set.beginGroup("NetConfig");
App::Mode = set.value("Mode").toString();
App::ServerIP = set.value("ServerIP").toString();
App::ServerPort = set.value("ServerPort").toInt();
App::ListenPort = set.value("ListenPort").toInt();
App::SleepTime = set.value("SleepTime").toInt();
App::AutoConnect = set.value("AutoConnect").toBool();
App::Mode = set.value("Mode", App::Mode).toString();
App::ServerIP = set.value("ServerIP", App::ServerIP).toString();
App::ServerPort = set.value("ServerPort", App::ServerPort).toInt();
App::ListenPort = set.value("ListenPort", App::ListenPort).toInt();
App::SleepTime = set.value("SleepTime", App::SleepTime).toInt();
App::AutoConnect = set.value("AutoConnect", App::AutoConnect).toBool();
set.endGroup();
}
@@ -96,20 +96,12 @@ void App::writeConfig()
set.endGroup();
}
void App::newConfig()
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
App::Parity = App::Parity.toLatin1();
#endif
writeConfig();
}
bool App::checkConfig()
{
//如果配置文件大小为0,则以初始值继续运行,并生成配置文件
QFile file(App::ConfigFile);
if (file.size() == 0) {
newConfig();
writeConfig();
return false;
}
@@ -121,7 +113,6 @@ bool App::checkConfig()
line = line.replace("\r", "");
line = line.replace("\n", "");
QStringList list = line.split("=");
if (list.count() == 2) {
if (list.at(1) == "") {
ok = false;
@@ -131,11 +122,11 @@ bool App::checkConfig()
}
if (!ok) {
newConfig();
writeConfig();
return false;
}
} else {
newConfig();
writeConfig();
return false;
}
@@ -166,6 +157,10 @@ void App::readSendData()
file.close();
}
if (App::Datas.count() == 0) {
App::Datas << "16 FF 01 01 E0 E1" << "16 FF 01 01 E1 E2";
}
}
void App::readDeviceData()

View File

@@ -36,7 +36,6 @@ public:
//读写配置参数及其他操作
static void readConfig(); //读取配置参数
static void writeConfig(); //写入配置参数
static void newConfig(); //以初始值新建配置文件
static bool checkConfig(); //校验配置文件
static QStringList Intervals;
@@ -45,7 +44,6 @@ public:
static QStringList Values;
static void readSendData();
static void readDeviceData();
};
#endif // APP_H

View File

@@ -11,6 +11,18 @@
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<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="enabled">
@@ -32,9 +44,15 @@
</item>
<item row="0" column="1" rowspan="2">
<widget class="QWidget" name="widgetRight" native="true">
<property name="minimumSize">
<size>
<width>180</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<width>180</width>
<height>16777215</height>
</size>
</property>
@@ -60,6 +78,18 @@
<enum>QFrame::Sunken</enum>
</property>
<layout class="QGridLayout" name="gridLayout_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 row="0" column="0">
<widget class="QLabel" name="labPortName">
<property name="text">
@@ -168,6 +198,18 @@
<string>串口配置</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_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 row="0" column="0">
<widget class="QCheckBox" name="ckHexSend">
<property name="text">
@@ -296,6 +338,18 @@
<string>网络配置</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<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" colspan="2">
<layout class="QGridLayout" name="gridLayout_4">
<item row="3" column="0">

View File

@@ -24,7 +24,8 @@ int main(int argc, char *argv[])
App::readDeviceData();
frmComTool w;
w.setWindowTitle("串口调试助手V2019 QQ: 517216493");
w.setWindowTitle("串口调试助手 V2021 (QQ: 517216493 WX: feiyangqingyun)");
QUIHelper::setFormInCenter(&w);
w.show();
return a.exec();

View File

@@ -1,9 +1,17 @@
HEADERS += \
$$PWD/app.h \
$$PWD/quiwidget.h \
$$PWD/tcpserver.h
HEADERS += $$PWD/app.h
HEADERS += $$PWD/quiwidget.h
HEADERS += $$PWD/tcpclient.h
HEADERS += $$PWD/tcpserver.h
SOURCES += \
$$PWD/app.cpp \
$$PWD/quiwidget.cpp \
$$PWD/tcpserver.cpp
SOURCES += $$PWD/app.cpp
SOURCES += $$PWD/quiwidget.cpp
SOURCES += $$PWD/tcpclient.cpp
SOURCES += $$PWD/tcpserver.cpp
contains(DEFINES, websocket) {
HEADERS += $$PWD/webclient.h
HEADERS += $$PWD/webserver.h
SOURCES += $$PWD/webclient.cpp
SOURCES += $$PWD/webserver.cpp
}

View File

@@ -24,7 +24,7 @@ bool App::AutoSendTcpServer = false;
int App::IntervalTcpServer = 1000;
QString App::TcpListenIP = "127.0.0.1";
int App::TcpListenPort = 6000;
bool App::SelectAllTcpServer = false;
bool App::SelectAllTcpServer = true;
bool App::HexSendUdpClient = false;
bool App::HexReceiveUdpClient = false;
@@ -45,6 +45,25 @@ QString App::UdpListenIP = "127.0.0.1";
int App::UdpListenPort = 6000;
bool App::SelectAllUdpServer = false;
bool App::HexSendWebClient = false;
bool App::HexReceiveWebClient = false;
bool App::AsciiWebClient = true;
bool App::DebugWebClient = false;
bool App::AutoSendWebClient = false;
int App::IntervalWebClient = 1000;
QString App::WebServerIP = "ws://127.0.0.1";
int App::WebServerPort = 6000;
bool App::HexSendWebServer = false;
bool App::HexReceiveWebServer = false;
bool App::AsciiWebServer = true;
bool App::DebugWebServer = false;
bool App::AutoSendWebServer = false;
int App::IntervalWebServer = 1000;
QString App::WebListenIP = "127.0.0.1";
int App::WebListenPort = 6000;
bool App::SelectAllWebServer = true;
void App::readConfig()
{
if (!checkConfig()) {
@@ -58,49 +77,72 @@ void App::readConfig()
set.endGroup();
set.beginGroup("TcpClientConfig");
App::HexSendTcpClient = set.value("HexSendTcpClient").toBool();
App::HexReceiveTcpClient = set.value("HexReceiveTcpClient").toBool();
App::AsciiTcpClient = set.value("AsciiTcpClient").toBool();
App::DebugTcpClient = set.value("DebugTcpClient").toBool();
App::AutoSendTcpClient = set.value("AutoSendTcpClient").toBool();
App::IntervalTcpClient = set.value("IntervalTcpClient").toInt();
App::TcpServerIP = set.value("TcpServerIP").toString();
App::TcpServerPort = set.value("TcpServerPort").toInt();
App::HexSendTcpClient = set.value("HexSendTcpClient", App::HexSendTcpClient).toBool();
App::HexReceiveTcpClient = set.value("HexReceiveTcpClient", App::HexReceiveTcpClient).toBool();
App::AsciiTcpClient = set.value("AsciiTcpClient", App::AsciiTcpClient).toBool();
App::DebugTcpClient = set.value("DebugTcpClient", App::DebugTcpClient).toBool();
App::AutoSendTcpClient = set.value("AutoSendTcpClient", App::AutoSendTcpClient).toBool();
App::IntervalTcpClient = set.value("IntervalTcpClient", App::IntervalTcpClient).toInt();
App::TcpServerIP = set.value("TcpServerIP", App::TcpServerIP).toString();
App::TcpServerPort = set.value("TcpServerPort", App::TcpServerPort).toInt();
set.endGroup();
set.beginGroup("TcpServerConfig");
App::HexSendTcpServer = set.value("HexSendTcpServer").toBool();
App::HexReceiveTcpServer = set.value("HexReceiveTcpServer").toBool();
App::AsciiTcpServer = set.value("AsciiTcpServer").toBool();
App::DebugTcpServer = set.value("DebugTcpServer").toBool();
App::AutoSendTcpServer = set.value("AutoSendTcpServer").toBool();
App::IntervalTcpServer = set.value("IntervalTcpServer").toInt();
App::TcpListenIP = set.value("TcpListenIP").toString();
App::TcpListenPort = set.value("TcpListenPort").toInt();
App::SelectAllTcpServer = set.value("SelectAllTcpServer").toBool();
App::HexSendTcpServer = set.value("HexSendTcpServer", App::HexSendTcpServer).toBool();
App::HexReceiveTcpServer = set.value("HexReceiveTcpServer", App::HexReceiveTcpServer).toBool();
App::AsciiTcpServer = set.value("AsciiTcpServer", App::AsciiTcpServer).toBool();
App::DebugTcpServer = set.value("DebugTcpServer", App::DebugTcpServer).toBool();
App::AutoSendTcpServer = set.value("AutoSendTcpServer", App::AutoSendTcpServer).toBool();
App::IntervalTcpServer = set.value("IntervalTcpServer", App::IntervalTcpServer).toInt();
App::TcpListenIP = set.value("TcpListenIP", App::TcpListenIP).toString();
App::TcpListenPort = set.value("TcpListenPort", App::TcpListenPort).toInt();
App::SelectAllTcpServer = set.value("SelectAllTcpServer", App::SelectAllTcpServer).toBool();
set.endGroup();
set.beginGroup("UdpClientConfig");
App::HexSendUdpClient = set.value("HexSendUdpClient").toBool();
App::HexReceiveUdpClient = set.value("HexReceiveUdpClient").toBool();
App::AsciiUdpClient = set.value("AsciiUdpClient").toBool();
App::DebugUdpClient = set.value("DebugUdpClient").toBool();
App::AutoSendUdpClient = set.value("AutoSendUdpClient").toBool();
App::IntervalUdpClient = set.value("IntervalUdpClient").toInt();
App::UdpServerIP = set.value("UdpServerIP").toString();
App::UdpServerPort = set.value("UdpServerPort").toInt();
App::HexSendUdpClient = set.value("HexSendUdpClient", App::HexSendUdpClient).toBool();
App::HexReceiveUdpClient = set.value("HexReceiveUdpClient", App::HexReceiveUdpClient).toBool();
App::AsciiUdpClient = set.value("AsciiUdpClient", App::AsciiUdpClient).toBool();
App::DebugUdpClient = set.value("DebugUdpClient", App::DebugUdpClient).toBool();
App::AutoSendUdpClient = set.value("AutoSendUdpClient", App::AutoSendUdpClient).toBool();
App::IntervalUdpClient = set.value("IntervalUdpClient", App::IntervalUdpClient).toInt();
App::UdpServerIP = set.value("UdpServerIP", App::UdpServerIP).toString();
App::UdpServerPort = set.value("UdpServerPort", App::UdpServerPort).toInt();
set.endGroup();
set.beginGroup("UdpServerConfig");
App::HexSendUdpServer = set.value("HexSendUdpServer").toBool();
App::HexReceiveUdpServer = set.value("HexReceiveUdpServer").toBool();
App::AsciiUdpServer = set.value("AsciiUdpServer").toBool();
App::DebugUdpServer = set.value("DebugUdpServer").toBool();
App::AutoSendUdpServer = set.value("AutoSendUdpServer").toBool();
App::IntervalUdpServer = set.value("IntervalUdpServer").toInt();
App::UdpListenIP = set.value("UdpListenIP").toString();
App::UdpListenPort = set.value("UdpListenPort").toInt();
App::SelectAllUdpServer = set.value("SelectAllUdpServer").toBool();
App::HexSendUdpServer = set.value("HexSendUdpServer", App::HexSendUdpServer).toBool();
App::HexReceiveUdpServer = set.value("HexReceiveUdpServer", App::HexReceiveUdpServer).toBool();
App::AsciiUdpServer = set.value("AsciiUdpServer", App::AsciiUdpServer).toBool();
App::DebugUdpServer = set.value("DebugUdpServer", App::DebugUdpServer).toBool();
App::AutoSendUdpServer = set.value("AutoSendUdpServer", App::AutoSendUdpServer).toBool();
App::IntervalUdpServer = set.value("IntervalUdpServer", App::IntervalUdpServer).toInt();
App::UdpListenIP = set.value("UdpListenIP", App::UdpListenIP).toString();
App::UdpListenPort = set.value("UdpListenPort", App::UdpListenPort).toInt();
App::SelectAllUdpServer = set.value("SelectAllUdpServer", App::SelectAllUdpServer).toBool();
set.endGroup();
set.beginGroup("WebClientConfig");
App::HexSendWebClient = set.value("HexSendWebClient", App::HexSendWebClient).toBool();
App::HexReceiveWebClient = set.value("HexReceiveWebClient", App::HexReceiveWebClient).toBool();
App::AsciiWebClient = set.value("AsciiWebClient", App::AsciiWebClient).toBool();
App::DebugWebClient = set.value("DebugWebClient", App::DebugWebClient).toBool();
App::AutoSendWebClient = set.value("AutoSendWebClient", App::AutoSendWebClient).toBool();
App::IntervalWebClient = set.value("IntervalWebClient", App::IntervalWebClient).toInt();
App::WebServerIP = set.value("WebServerIP", App::WebServerIP).toString();
App::WebServerPort = set.value("WebServerPort", App::WebServerPort).toInt();
set.endGroup();
set.beginGroup("WebServerConfig");
App::HexSendWebServer = set.value("HexSendWebServer", App::HexSendWebServer).toBool();
App::HexReceiveWebServer = set.value("HexReceiveWebServer", App::HexReceiveWebServer).toBool();
App::AsciiWebServer = set.value("AsciiWebServer", App::AsciiWebServer).toBool();
App::DebugWebServer = set.value("DebugWebServer", App::DebugWebServer).toBool();
App::AutoSendWebServer = set.value("AutoSendWebServer", App::AutoSendWebServer).toBool();
App::IntervalWebServer = set.value("IntervalWebServer", App::IntervalWebServer).toInt();
App::WebListenIP = set.value("WebListenIP", App::WebListenIP).toString();
App::WebListenPort = set.value("WebListenPort", App::WebListenPort).toInt();
App::SelectAllWebServer = set.value("SelectAllWebServer", App::SelectAllWebServer).toBool();
set.endGroup();
}
@@ -126,8 +168,8 @@ void App::writeConfig()
set.setValue("HexSendTcpServer", App::HexSendTcpServer);
set.setValue("HexReceiveTcpServer", App::HexReceiveTcpServer);
set.setValue("DebugTcpServer", App::DebugTcpServer);
set.setValue("AutoSendTcpServer", App::AutoSendTcpServer);
set.setValue("IntervalTcpServer", App::IntervalTcpServer);
set.setValue("AutoSendTcpServer", App::AutoSendTcpServer);
set.setValue("IntervalTcpServer", App::IntervalTcpServer);
set.setValue("TcpListenIP", App::TcpListenIP);
set.setValue("TcpListenPort", App::TcpListenPort);
set.setValue("SelectAllTcpServer", App::SelectAllTcpServer);
@@ -148,18 +190,32 @@ void App::writeConfig()
set.setValue("HexReceiveUdpServer", App::HexReceiveUdpServer);
set.setValue("DebugUdpServer", App::DebugUdpServer);
set.setValue("AutoSendUdpServer", App::AutoSendUdpServer);
set.setValue("IntervalUdpServer", App::IntervalUdpServer);
set.setValue("IntervalUdpServer", App::IntervalUdpServer);
set.setValue("UdpListenIP", App::UdpListenIP);
set.setValue("UdpListenPort", App::UdpListenPort);
set.setValue("SelectAllUdpServer", App::SelectAllUdpServer);
set.endGroup();
}
set.endGroup();
void App::newConfig()
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#endif
writeConfig();
set.beginGroup("WebClientConfig");
set.setValue("HexSendWebClient", App::HexSendWebClient);
set.setValue("HexReceiveWebClient", App::HexReceiveWebClient);
set.setValue("DebugWebClient", App::DebugWebClient);
set.setValue("AutoSendWebClient", App::AutoSendWebClient);
set.setValue("IntervalWebClient", App::IntervalWebClient);
set.setValue("WebServerIP", App::WebServerIP);
set.setValue("WebServerPort", App::WebServerPort);
set.endGroup();
set.beginGroup("WebServerConfig");
set.setValue("HexSendWebServer", App::HexSendWebServer);
set.setValue("HexReceiveWebServer", App::HexReceiveWebServer);
set.setValue("DebugWebServer", App::DebugWebServer);
set.setValue("AutoSendWebServer", App::AutoSendWebServer);
set.setValue("IntervalWebServer", App::IntervalWebServer);
set.setValue("WebListenIP", App::WebListenIP);
set.setValue("WebListenPort", App::WebListenPort);
set.setValue("SelectAllWebServer", App::SelectAllWebServer);
set.endGroup();
}
bool App::checkConfig()
@@ -167,7 +223,7 @@ bool App::checkConfig()
//如果配置文件大小为0,则以初始值继续运行,并生成配置文件
QFile file(App::ConfigFile);
if (file.size() == 0) {
newConfig();
writeConfig();
return false;
}
@@ -179,7 +235,6 @@ bool App::checkConfig()
line = line.replace("\r", "");
line = line.replace("\n", "");
QStringList list = line.split("=");
if (list.count() == 2) {
if (list.at(1) == "") {
ok = false;
@@ -189,11 +244,11 @@ bool App::checkConfig()
}
if (!ok) {
newConfig();
writeConfig();
return false;
}
} else {
newConfig();
writeConfig();
return false;
}
@@ -224,6 +279,10 @@ void App::readSendData()
file.close();
}
if (App::Datas.count() == 0) {
App::Datas << "16 FF 01 01 E0 E1" << "16 FF 01 01 E1 E2";
}
}
void App::readDeviceData()
@@ -257,3 +316,17 @@ void App::readDeviceData()
file.close();
}
}
void App::saveData(const QString &data)
{
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();
}
}

View File

@@ -54,10 +54,30 @@ public:
static int UdpListenPort; //监听端口
static bool SelectAllUdpServer; //选中所有
//WEB客户端配置参数
static bool HexSendWebClient; //16进制发送
static bool HexReceiveWebClient; //16进制接收
static bool AsciiWebClient; //ASCII模式
static bool DebugWebClient; //启用数据调试
static bool AutoSendWebClient; //自动发送数据
static int IntervalWebClient; //发送数据间隔
static QString WebServerIP; //服务器IP
static int WebServerPort; //服务器端口
//WEB服务器配置参数
static bool HexSendWebServer; //16进制发送
static bool HexReceiveWebServer; //16进制接收
static bool AsciiWebServer; //ASCII模式
static bool DebugWebServer; //启用数据调试
static bool AutoSendWebServer; //自动发送数据
static int IntervalWebServer; //发送数据间隔
static QString WebListenIP; //监听地址
static int WebListenPort; //监听端口
static bool SelectAllWebServer; //选中所有
//读写配置参数及其他操作
static void readConfig(); //读取配置参数
static void writeConfig(); //写入配置参数
static void newConfig(); //以初始值新建配置文件
static bool checkConfig(); //校验配置文件
static QStringList Intervals;
@@ -66,6 +86,7 @@ public:
static QStringList Values;
static void readSendData();
static void readDeviceData();
static void saveData(const QString &data);
};
#endif // APP_H

81
nettool/api/tcpclient.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include "tcpclient.h"
#include "quiwidget.h"
TcpClient::TcpClient(QTcpSocket *socket, QObject *parent) : QObject(parent)
{
this->socket = socket;
ip = socket->peerAddress().toString();
ip = ip.replace("::ffff:", "");
port = socket->peerPort();
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
QString TcpClient::getIP() const
{
return this->ip;
}
int TcpClient::getPort() const
{
return this->port;
}
void TcpClient::disconnected()
{
socket->deleteLater();
this->deleteLater();
emit clientDisconnected();
}
void TcpClient::readData()
{
QByteArray data = socket->readAll();
if (data.length() <= 0) {
return;
}
QString buffer;
if (App::HexReceiveTcpServer) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else if (App::AsciiTcpServer) {
buffer = QUIHelper::byteArrayToAsciiStr(data);
} else {
buffer = QString(data);
}
emit receiveData(ip, port, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
if (App::DebugTcpServer) {
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 TcpClient::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexSendTcpServer) {
buffer = QUIHelper::hexStrToByteArray(data);
} else if (App::AsciiTcpServer) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toUtf8();
}
socket->write(buffer);
emit sendData(ip, port, data);
}
void TcpClient::abort()
{
socket->abort();
}

42
nettool/api/tcpclient.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QtNetwork>
//为了方便并发和单独处理数据所以单独写个类专门处理
#if 1
//小数据量可以不用线程,收发数据本身默认异步的
class TcpClient : public QObject
#else
//线程方便处理密集运算比如解析图片
class TcpClient : public QThread
#endif
{
Q_OBJECT
public:
explicit TcpClient(QTcpSocket *socket, QObject *parent = 0);
private:
QTcpSocket *socket;
QString ip;
int port;
public:
QString getIP() const;
int getPort() const;
private slots:
void disconnected();
void readData();
signals:
void clientDisconnected();
void sendData(const QString &ip, int port, const QString &data);
void receiveData(const QString &ip, int port, const QString &data);
public slots:
void sendData(const QString &data);
void abort();
};
#endif // TCPCLIENT_H

View File

@@ -1,102 +1,21 @@
#include "tcpserver.h"
#include "quiwidget.h"
TcpClient::TcpClient(QObject *parent) : QTcpSocket(parent)
{
ip = "127.0.0.1";
port = 6000;
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(deleteLater()));
connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
connect(this, SIGNAL(readyRead()), this, SLOT(readData()));
}
void TcpClient::setIP(const QString &ip)
{
this->ip = ip;
}
QString TcpClient::getIP() const
{
return this->ip;
}
void TcpClient::setPort(int port)
{
this->port = port;
}
int TcpClient::getPort() const
{
return this->port;
}
void TcpClient::readData()
{
QByteArray data = this->readAll();
if (data.length() <= 0) {
return;
}
QString buffer;
if (App::HexReceiveTcpServer) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else if (App::AsciiTcpServer) {
buffer = QUIHelper::byteArrayToAsciiStr(data);
} else {
buffer = QString(data);
}
emit receiveData(ip, port, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
if (App::DebugTcpServer) {
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 TcpClient::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexSendTcpServer) {
buffer = QUIHelper::hexStrToByteArray(data);
} else if (App::AsciiTcpServer) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toLatin1();
}
this->write(buffer);
emit sendData(ip, port, data);
}
TcpServer::TcpServer(QObject *parent) : QTcpServer(parent)
{
connect(this, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
void TcpServer::incomingConnection(qintptr handle)
#else
void TcpServer::incomingConnection(int handle)
#endif
void TcpServer::newConnection()
{
TcpClient *client = new TcpClient(this);
client->setSocketDescriptor(handle);
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
QTcpSocket *socket = this->nextPendingConnection();
TcpClient *client = new TcpClient(socket, this);
connect(client, SIGNAL(clientDisconnected()), this, SLOT(disconnected()));
connect(client, SIGNAL(sendData(QString, int, QString)), this, SIGNAL(sendData(QString, int, QString)));
connect(client, SIGNAL(receiveData(QString, int, QString)), this, SIGNAL(receiveData(QString, int, QString)));
QString ip = client->peerAddress().toString();
ip = ip.replace("::ffff:", "");
int port = client->peerPort();
client->setIP(ip);
client->setPort(port);
QString ip = client->getIP();
int port = client->getPort();
emit clientConnected(ip, port);
emit sendData(ip, port, "客户端上线");
@@ -131,7 +50,7 @@ void TcpServer::stop()
void TcpServer::writeData(const QString &ip, int port, const QString &data)
{
foreach (TcpClient *client, clients) {
if (client->peerAddress().toString() == ip && client->peerPort() == port) {
if (client->getIP() == ip && client->getPort() == port) {
client->sendData(data);
break;
}
@@ -148,8 +67,8 @@ void TcpServer::writeData(const QString &data)
void TcpServer::remove(const QString &ip, int port)
{
foreach (TcpClient *client, clients) {
if (client->peerAddress().toString() == ip && client->peerPort() == port) {
client->disconnectFromHost();
if (client->getIP() == ip && client->getPort() == port) {
client->abort();
break;
}
}
@@ -158,6 +77,6 @@ void TcpServer::remove(const QString &ip, int port)
void TcpServer::remove()
{
foreach (TcpClient *client, clients) {
client->disconnectFromHost();
client->abort();
}
}

View File

@@ -1,36 +1,7 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QtNetwork>
class TcpClient : public QTcpSocket
{
Q_OBJECT
public:
explicit TcpClient(QObject *parent = 0);
private:
QString ip;
int port;
public:
void setIP(const QString &ip);
QString getIP()const;
void setPort(int port);
int getPort()const;
private slots:
void readData();
signals:
void sendData(const QString &ip, int port, const QString &data);
void receiveData(const QString &ip, int port, const QString &data);
public slots:
void sendData(const QString &data);
};
#include "tcpclient.h"
class TcpServer : public QTcpServer
{
@@ -41,14 +12,8 @@ public:
private:
QList<TcpClient *> clients;
protected:
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
void incomingConnection(qintptr handle);
#else
void incomingConnection(int handle);
#endif
private slots:
void newConnection();
void disconnected();
signals:
@@ -73,7 +38,6 @@ public slots:
void remove(const QString &ip, int port);
//断开所有连接
void remove();
};
#endif // TCPSERVER_H

99
nettool/api/webclient.cpp Normal file
View File

@@ -0,0 +1,99 @@
#include "webclient.h"
#include "quiwidget.h"
WebClient::WebClient(QWebSocket *socket, QObject *parent) : QObject(parent)
{
this->socket = socket;
ip = socket->peerAddress().toString();
ip = ip.replace("::ffff:", "");
port = socket->peerPort();
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
//暂时使用前面两个信号,部分系统上后面两个信号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)));
}
QString WebClient::getIP() const
{
return this->ip;
}
int WebClient::getPort() const
{
return this->port;
}
void WebClient::disconnected()
{
socket->deleteLater();
this->deleteLater();
emit clientDisconnected();
}
void WebClient::textFrameReceived(const QString &data, bool isLastFrame)
{
QString buffer = data;
emit receiveData(ip, port, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
if (App::DebugWebServer) {
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 WebClient::binaryFrameReceived(const QByteArray &data, bool isLastFrame)
{
QString buffer;
if (App::HexReceiveWebClient) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else {
buffer = QString(data);
}
textFrameReceived(buffer, isLastFrame);
}
void WebClient::textMessageReceived(const QString &data)
{
textFrameReceived(data, true);
}
void WebClient::binaryMessageReceived(const QByteArray &data)
{
binaryFrameReceived(data, true);
}
void WebClient::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexSendWebServer) {
buffer = QUIHelper::hexStrToByteArray(data);
} else {
buffer = data.toUtf8();
}
if (App::AsciiWebServer) {
socket->sendTextMessage(data);
} else {
socket->sendBinaryMessage(buffer);
}
emit sendData(ip, port, data);
}
void WebClient::abort()
{
socket->abort();
}

45
nettool/api/webclient.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef WEBCLIENT_H
#define WEBCLIENT_H
#include <QtWebSockets>
//为了方便并发和单独处理数据所以单独写个类专门处理
#if 1
//小数据量可以不用线程,收发数据本身默认异步的
class WebClient : public QObject
#else
//线程方便处理密集运算比如解析图片
class WebClient : public QThread
#endif
{
Q_OBJECT
public:
explicit WebClient(QWebSocket *socket, QObject *parent = 0);
private:
QWebSocket *socket;
QString ip;
int port;
public:
QString getIP() const;
int getPort() const;
private slots:
void disconnected();
void textFrameReceived(const QString &data, bool isLastFrame);
void binaryFrameReceived(const QByteArray &data, bool isLastFrame);
void textMessageReceived(const QString &data);
void binaryMessageReceived(const QByteArray &data);
signals:
void clientDisconnected();
void sendData(const QString &ip, int port, const QString &data);
void receiveData(const QString &ip, int port, const QString &data);
public slots:
void sendData(const QString &data);
void abort();
};
#endif // WEBCLIENT_H

82
nettool/api/webserver.cpp Normal file
View File

@@ -0,0 +1,82 @@
#include "webserver.h"
#include "quiwidget.h"
WebServer::WebServer(const QString &serverName, SslMode secureMode, QObject *parent) : QWebSocketServer(serverName, secureMode, parent)
{
connect(this, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
void WebServer::newConnection()
{
QWebSocket *socket = this->nextPendingConnection();
WebClient *client = new WebClient(socket, this);
connect(client, SIGNAL(clientDisconnected()), this, SLOT(disconnected()));
connect(client, SIGNAL(sendData(QString, int, QString)), this, SIGNAL(sendData(QString, int, QString)));
connect(client, SIGNAL(receiveData(QString, int, QString)), this, SIGNAL(receiveData(QString, int, QString)));
QString ip = client->getIP();
int port = client->getPort();
emit clientConnected(ip, port);
emit sendData(ip, port, "客户端上线");
//连接后加入链表
clients.append(client);
}
void WebServer::disconnected()
{
WebClient *client = (WebClient *)sender();
QString ip = client->getIP();
int port = client->getPort();
emit clientDisconnected(ip, port);
emit sendData(ip, port, "客户端下线");
//断开连接后从链表中移除
clients.removeOne(client);
}
bool WebServer::start()
{
bool ok = listen(QHostAddress(App::WebListenIP), App::WebListenPort);
return ok;
}
void WebServer::stop()
{
remove();
this->close();
}
void WebServer::writeData(const QString &ip, int port, const QString &data)
{
foreach (WebClient *client, clients) {
if (client->getIP() == ip && client->getPort() == port) {
client->sendData(data);
break;
}
}
}
void WebServer::writeData(const QString &data)
{
foreach (WebClient *client, clients) {
client->sendData(data);
}
}
void WebServer::remove(const QString &ip, int port)
{
foreach (WebClient *client, clients) {
if (client->getIP() == ip && client->getPort() == port) {
client->abort();
break;
}
}
}
void WebServer::remove()
{
foreach (WebClient *client, clients) {
client->abort();
}
}

43
nettool/api/webserver.h Normal file
View File

@@ -0,0 +1,43 @@
#ifndef WEBSERVER_H
#define WEBSERVER_H
#include "webclient.h"
class WebServer : public QWebSocketServer
{
Q_OBJECT
public:
explicit WebServer(const QString &serverName = QString(), QWebSocketServer::SslMode secureMode = QWebSocketServer::NonSecureMode, QObject *parent = 0);
private:
QList<WebClient *> clients;
private slots:
void newConnection();
void disconnected();
signals:
void sendData(const QString &ip, int port, const QString &data);
void receiveData(const QString &ip, int port, const QString &data);
void clientConnected(const QString &ip, int port);
void clientDisconnected(const QString &ip, int port);
public slots:
//启动服务
bool start();
//停止服务
void stop();
//指定连接发送数据
void writeData(const QString &ip, int port, const QString &data);
//对所有连接发送数据
void writeData(const QString &data);
//断开指定连接
void remove(const QString &ip, int port);
//断开所有连接
void remove();
};
#endif // WEBSERVER_H

View File

@@ -1,20 +1,28 @@
FORMS += \
$$PWD/frmmain.ui \
$$PWD/frmtcpclient.ui \
$$PWD/frmtcpserver.ui \
$$PWD/frmudpclient.ui \
$$PWD/frmudpserver.ui
FORMS += $$PWD/frmmain.ui
FORMS += $$PWD/frmtcpclient.ui
FORMS += $$PWD/frmtcpserver.ui
FORMS += $$PWD/frmudpclient.ui
FORMS += $$PWD/frmudpserver.ui
HEADERS += \
$$PWD/frmmain.h \
$$PWD/frmtcpclient.h \
$$PWD/frmtcpserver.h \
$$PWD/frmudpclient.h \
$$PWD/frmudpserver.h
HEADERS += $$PWD/frmmain.h
HEADERS += $$PWD/frmtcpclient.h
HEADERS += $$PWD/frmtcpserver.h
HEADERS += $$PWD/frmudpclient.h
HEADERS += $$PWD/frmudpserver.h
SOURCES += \
$$PWD/frmmain.cpp \
$$PWD/frmtcpclient.cpp \
$$PWD/frmtcpserver.cpp \
$$PWD/frmudpclient.cpp \
$$PWD/frmudpserver.cpp
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
}

View File

@@ -2,10 +2,20 @@
#include "ui_frmmain.h"
#include "quiwidget.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);
ui->tabWidget->setCurrentIndex(App::CurrentIndex);
this->initForm();
this->initConfig();
}
frmMain::~frmMain()
@@ -13,8 +23,29 @@ frmMain::~frmMain()
delete ui;
}
void frmMain::on_tabWidget_currentChanged(int index)
void frmMain::initForm()
{
App::CurrentIndex = index;
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
App::CurrentIndex = 4;
#endif
}
void frmMain::initConfig()
{
ui->tabWidget->setCurrentIndex(App::CurrentIndex);
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(saveConfig()));
}
void frmMain::saveConfig()
{
App::CurrentIndex = ui->tabWidget->currentIndex();
App::writeConfig();
}

View File

@@ -15,11 +15,13 @@ public:
explicit frmMain(QWidget *parent = 0);
~frmMain();
private slots:
void on_tabWidget_currentChanged(int index);
private:
Ui::frmMain *ui;
private slots:
void initForm();
void initConfig();
void saveConfig();
};
#endif // FRMMAIN_H

View File

@@ -35,58 +35,12 @@
<enum>QTabWidget::South</enum>
</property>
<property name="currentIndex">
<number>2</number>
<number>-1</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="frmUdpClient" name="tabUdpClient">
<attribute name="title">
<string>UDP客户端</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>
<customwidget>
<class>frmUdpClient</class>
<extends>QWidget</extends>
<header>frmudpclient.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -6,7 +6,7 @@ frmTcpClient::frmTcpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp
{
ui->setupUi(this);
this->initForm();
this->initConfig();
this->initConfig();
}
frmTcpClient::~frmTcpClient()
@@ -17,11 +17,11 @@ frmTcpClient::~frmTcpClient()
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()));
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
@@ -138,14 +138,14 @@ void frmTcpClient::connected()
void frmTcpClient::disconnected()
{
isOk = false;
tcpSocket->abort();
socket->abort();
ui->btnConnect->setText("连接");
append(1, "服务器断开");
}
void frmTcpClient::readData()
{
QByteArray data = tcpSocket->readAll();
QByteArray data = socket->readAll();
if (data.length() <= 0) {
return;
}
@@ -181,37 +181,27 @@ void frmTcpClient::sendData(const QString &data)
} else if (App::AsciiTcpClient) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toLatin1();
buffer = data.toUtf8();
}
tcpSocket->write(buffer);
socket->write(buffer);
append(0, data);
}
void frmTcpClient::on_btnConnect_clicked()
{
if (ui->btnConnect->text() == "连接") {
tcpSocket->abort();
tcpSocket->connectToHost(App::TcpServerIP, App::TcpServerPort);
socket->abort();
socket->connectToHost(App::TcpServerIP, App::TcpServerPort);
} else {
tcpSocket->abort();
socket->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();
}
App::saveData(data);
on_btnClear_clicked();
}

View File

@@ -20,7 +20,7 @@ private:
Ui::frmTcpClient *ui;
bool isOk;
QTcpSocket *tcpSocket;
QTcpSocket *socket;
QTimer *timer;
private slots:
@@ -30,6 +30,7 @@ private slots:
void changeTimer();
void append(int type, const QString &data, bool clear = false);
private slots:
void connected();
void disconnected();
void readData();

View File

@@ -14,6 +14,18 @@
<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">
@@ -43,16 +55,16 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="leftMargin">
<number>9</number>
<number>6</number>
</property>
<property name="topMargin">
<number>9</number>
<number>6</number>
</property>
<property name="rightMargin">
<number>9</number>
<number>6</number>
</property>
<property name="bottomMargin">
<number>9</number>
<number>6</number>
</property>
<item>
<widget class="QCheckBox" name="ckHexReceive">
@@ -71,7 +83,7 @@
<item>
<widget class="QCheckBox" name="ckAscii">
<property name="text">
<string>Ascii控制字符</string>
<string>控制字符</string>
</property>
</widget>
</item>

View File

@@ -6,7 +6,6 @@ frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp
{
ui->setupUi(this);
this->initForm();
this->initIP();
this->initConfig();
}
@@ -18,26 +17,24 @@ frmTcpServer::~frmTcpServer()
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)));
server = new TcpServer(this);
connect(server, SIGNAL(clientConnected(QString, int)), this, SLOT(clientConnected(QString, int)));
connect(server, SIGNAL(clientDisconnected(QString, int)), this, SLOT(clientDisconnected(QString, int)));
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(App::Intervals);
ui->cboxData->addItems(App::Datas);
QUIHelper::setLabStyle(ui->labCount, 3);
}
void frmTcpServer::initIP()
{
//获取本机所有IP
QStringList ips = QUIHelper::getLocalIPs();
ui->cboxListenIP->addItems(ips);
ui->cboxListenIP->addItem("127.0.0.1");
if (!ips.contains("127.0.0.1")) {
ui->cboxListenIP->addItem("127.0.0.1");
}
}
void frmTcpServer::initConfig()
@@ -180,14 +177,14 @@ void frmTcpServer::receiveData(const QString &ip, int port, const QString &data)
void frmTcpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
isOk = tcpServer->start();
isOk = server->start();
if (isOk) {
append(0, "监听成功");
ui->btnListen->setText("关闭");
}
} else {
isOk = false;
tcpServer->stop();
server->stop();
ui->btnListen->setText("监听");
}
}
@@ -195,17 +192,7 @@ void frmTcpServer::on_btnListen_clicked()
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();
}
App::saveData(data);
on_btnClear_clicked();
}
@@ -226,13 +213,13 @@ void frmTcpServer::on_btnSend_clicked()
}
if (ui->ckSelectAll->isChecked()) {
tcpServer->writeData(data);
server->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);
server->writeData(list.at(0), list.at(1).toInt(), data);
}
}
}
@@ -240,13 +227,13 @@ void frmTcpServer::on_btnSend_clicked()
void frmTcpServer::on_btnClose_clicked()
{
if (ui->ckSelectAll->isChecked()) {
tcpServer->remove();
server->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());
server->remove(list.at(0), list.at(1).toInt());
}
}
}

View File

@@ -17,24 +17,24 @@ public:
~frmTcpServer();
private:
Ui::frmTcpServer *ui;
Ui::frmTcpServer *ui;
bool isOk;
TcpServer *tcpServer;
TcpServer *server;
QTimer *timer;
private slots:
void initForm();
void initIP();
void initForm();
void initConfig();
void saveConfig();
void changeTimer();
void append(int type, const QString &data, bool clear = false);
private slots:
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);
void receiveData(const QString &ip, int port, const QString &data);
private slots:
void on_btnListen_clicked();

View File

@@ -14,6 +14,18 @@
<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">
@@ -43,16 +55,16 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>9</number>
<number>6</number>
</property>
<property name="topMargin">
<number>9</number>
<number>6</number>
</property>
<property name="rightMargin">
<number>9</number>
<number>6</number>
</property>
<property name="bottomMargin">
<number>9</number>
<number>6</number>
</property>
<item>
<widget class="QCheckBox" name="ckHexReceive">
@@ -71,7 +83,7 @@
<item>
<widget class="QCheckBox" name="ckAscii">
<property name="text">
<string>Ascii控制字符</string>
<string>控制字符</string>
</property>
</widget>
</item>

View File

@@ -16,8 +16,8 @@ frmUdpClient::~frmUdpClient()
void frmUdpClient::initForm()
{
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
socket = new QUdpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
@@ -131,9 +131,9 @@ void frmUdpClient::readData()
QByteArray data;
QString buffer;
while (udpSocket->hasPendingDatagrams()) {
data.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(data.data(), data.size(), &host, &port);
while (socket->hasPendingDatagrams()) {
data.resize(socket->pendingDatagramSize());
socket->readDatagram(data.data(), data.size(), &host, &port);
if (App::HexReceiveUdpClient) {
buffer = QUIHelper::byteArrayToHexStr(data);
@@ -172,10 +172,10 @@ void frmUdpClient::sendData(const QString &ip, int port, const QString &data)
} else if (App::AsciiUdpClient) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toLatin1();
buffer = data.toUtf8();
}
udpSocket->writeDatagram(buffer, QHostAddress(ip), port);
socket->writeDatagram(buffer, QHostAddress(ip), port);
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
append(0, str);
@@ -184,17 +184,7 @@ void frmUdpClient::sendData(const QString &ip, int port, const QString &data)
void frmUdpClient::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();
}
App::saveData(data);
on_btnClear_clicked();
}

View File

@@ -19,7 +19,7 @@ public:
private:
Ui::frmUdpClient *ui;
QUdpSocket *udpSocket;
QUdpSocket *socket;
QTimer *timer;
private slots:
@@ -29,6 +29,7 @@ private slots:
void changeTimer();
void append(int type, const QString &data, bool clear = false);
private slots:
void readData();
void sendData(const QString &ip, int port, const QString &data);

View File

@@ -14,6 +14,18 @@
<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">
@@ -42,6 +54,18 @@
<enum>QFrame::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<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">
@@ -59,7 +83,7 @@
<item>
<widget class="QCheckBox" name="ckAscii">
<property name="text">
<string>Ascii控制字符</string>
<string>控制字符</string>
</property>
</widget>
</item>

View File

@@ -6,7 +6,6 @@ frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdp
{
ui->setupUi(this);
this->initForm();
this->initIP();
this->initConfig();
}
@@ -17,23 +16,21 @@ frmUdpServer::~frmUdpServer()
void frmUdpServer::initForm()
{
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
socket = new QUdpSocket(this);
connect(socket, 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);
QUIHelper::setLabStyle(ui->labCount, 3);
}
void frmUdpServer::initIP()
{
//获取本机所有IP
QStringList ips = QUIHelper::getLocalIPs();
ui->cboxListenIP->addItems(ips);
ui->cboxListenIP->addItem("127.0.0.1");
if (!ips.contains("127.0.0.1")) {
ui->cboxListenIP->addItem("127.0.0.1");
}
}
void frmUdpServer::initConfig()
@@ -145,9 +142,9 @@ void frmUdpServer::readData()
QByteArray data;
QString buffer;
while (udpSocket->hasPendingDatagrams()) {
data.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(data.data(), data.size(), &host, &port);
while (socket->hasPendingDatagrams()) {
data.resize(socket->pendingDatagramSize());
socket->readDatagram(data.data(), data.size(), &host, &port);
if (App::HexReceiveUdpServer) {
buffer = QUIHelper::byteArrayToHexStr(data);
@@ -187,10 +184,10 @@ void frmUdpServer::sendData(const QString &ip, int port, const QString &data)
} else if (App::AsciiUdpServer) {
buffer = QUIHelper::asciiStrToByteArray(data);
} else {
buffer = data.toLatin1();
buffer = data.toUtf8();
}
udpSocket->writeDatagram(buffer, QHostAddress(ip), port);
socket->writeDatagram(buffer, QHostAddress(ip), port);
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data);
append(0, str);
@@ -214,13 +211,13 @@ void frmUdpServer::clientConnected(const QString &ip, int port)
void frmUdpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
bool ok = udpSocket->bind(QHostAddress(App::UdpListenIP), App::UdpListenPort);
bool ok = socket->bind(QHostAddress(App::UdpListenIP), App::UdpListenPort);
if (ok) {
ui->btnListen->setText("关闭");
append(0, "监听成功");
}
} else {
udpSocket->abort();
socket->abort();
ui->btnListen->setText("监听");
}
}
@@ -228,17 +225,7 @@ void frmUdpServer::on_btnListen_clicked()
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();
}
App::saveData(data);
on_btnClear_clicked();
}

View File

@@ -17,19 +17,19 @@ public:
~frmUdpServer();
private:
Ui::frmUdpServer *ui;
Ui::frmUdpServer *ui;
QUdpSocket *udpSocket;
QUdpSocket *socket;
QTimer *timer;
private slots:
void initForm();
void initIP();
void initConfig();
void saveConfig();
void changeTimer();
void append(int type, const QString &data, bool clear = false);
private slots:
void readData();
void sendData(const QString &ip, int port, const QString &data);
void clientConnected(const QString &ip, int port);

View File

@@ -14,6 +14,18 @@
<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">
@@ -42,6 +54,18 @@
<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">
@@ -59,7 +83,7 @@
<item>
<widget class="QCheckBox" name="ckAscii">
<property name="text">
<string>Ascii控制字符</string>
<string>控制字符</string>
</property>
</widget>
</item>

View File

@@ -0,0 +1,244 @@
#include "frmwebclient.h"
#include "ui_frmwebclient.h"
#include "quiwidget.h"
frmWebClient::frmWebClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmWebClient)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmWebClient::~frmWebClient()
{
delete ui;
}
void frmWebClient::initForm()
{
isOk = false;
socket = new QWebSocket("WebSocket", QWebSocketProtocol::VersionLatest, this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
//暂时使用前面两个信号,部分系统上后面两个信号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(App::Intervals);
ui->cboxData->addItems(App::Datas);
}
void frmWebClient::initConfig()
{
ui->ckHexSend->setChecked(App::HexSendWebClient);
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckHexReceive->setChecked(App::HexReceiveWebClient);
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAscii->setChecked(App::AsciiWebClient);
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckDebug->setChecked(App::DebugWebClient);
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAutoSend->setChecked(App::AutoSendWebClient);
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalWebClient)));
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtServerIP->setText(App::WebServerIP);
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtServerPort->setText(QString::number(App::WebServerPort));
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
this->changeTimer();
}
void frmWebClient::saveConfig()
{
App::HexSendWebClient = ui->ckHexSend->isChecked();
App::HexReceiveWebClient = ui->ckHexReceive->isChecked();
App::AsciiWebClient = ui->ckAscii->isChecked();
App::DebugWebClient = ui->ckDebug->isChecked();
App::AutoSendWebClient = ui->ckAutoSend->isChecked();
App::IntervalWebClient = ui->cboxInterval->currentText().toInt();
App::WebServerIP = ui->txtServerIP->text().trimmed();
App::WebServerPort = ui->txtServerPort->text().trimmed().toInt();
App::writeConfig();
this->changeTimer();
}
void frmWebClient::changeTimer()
{
timer->setInterval(App::IntervalWebClient);
if (App::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("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 frmWebClient::connected()
{
isOk = true;
ui->btnConnect->setText("断开");
append(0, "服务器连接");
}
void frmWebClient::disconnected()
{
isOk = false;
socket->abort();
ui->btnConnect->setText("连接");
append(1, "服务器断开");
}
void frmWebClient::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexSendWebClient) {
buffer = QUIHelper::hexStrToByteArray(data);
} else {
buffer = data.toUtf8();
}
if (App::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 (App::DebugWebClient) {
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 frmWebClient::binaryFrameReceived(const QByteArray &data, bool isLastFrame)
{
QString buffer;
if (App::HexReceiveWebClient) {
buffer = QUIHelper::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(App::WebServerIP).arg(App::WebServerPort);
socket->abort();
socket->open(QUrl(url));
} else {
socket->abort();
}
}
void frmWebClient::on_btnSave_clicked()
{
QString data = ui->txtMain->toPlainText();
App::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);
}

View File

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

View 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>

View File

@@ -0,0 +1,239 @@
#include "frmwebserver.h"
#include "ui_frmwebserver.h"
#include "quiwidget.h"
frmWebServer::frmWebServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmWebServer)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmWebServer::~frmWebServer()
{
delete ui;
}
void frmWebServer::initForm()
{
isOk = false;
server = new WebServer("WebServer", QWebSocketServer::NonSecureMode, this);
connect(server, SIGNAL(clientConnected(QString, int)), this, SLOT(clientConnected(QString, int)));
connect(server, SIGNAL(clientDisconnected(QString, int)), this, SLOT(clientDisconnected(QString, int)));
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(App::Intervals);
ui->cboxData->addItems(App::Datas);
//获取本机所有IP
QStringList ips = QUIHelper::getLocalIPs();
ui->cboxListenIP->addItems(ips);
if (!ips.contains("127.0.0.1")) {
ui->cboxListenIP->addItem("127.0.0.1");
}
}
void frmWebServer::initConfig()
{
ui->ckHexSend->setChecked(App::HexSendWebServer);
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckHexReceive->setChecked(App::HexReceiveWebServer);
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAscii->setChecked(App::AsciiWebServer);
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckDebug->setChecked(App::DebugWebServer);
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->ckAutoSend->setChecked(App::AutoSendWebServer);
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(App::IntervalWebServer)));
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(App::WebListenIP));
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtListenPort->setText(QString::number(App::WebListenPort));
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->ckSelectAll->setChecked(App::SelectAllWebServer);
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
this->changeTimer();
}
void frmWebServer::saveConfig()
{
App::HexSendWebServer = ui->ckHexSend->isChecked();
App::HexReceiveWebServer = ui->ckHexReceive->isChecked();
App::AsciiWebServer = ui->ckAscii->isChecked();
App::DebugWebServer = ui->ckDebug->isChecked();
App::AutoSendWebServer = ui->ckAutoSend->isChecked();
App::IntervalWebServer = ui->cboxInterval->currentText().toInt();
App::WebListenIP = ui->cboxListenIP->currentText();
App::WebListenPort = ui->txtListenPort->text().trimmed().toInt();
App::SelectAllWebServer = ui->ckSelectAll->isChecked();
App::writeConfig();
this->changeTimer();
}
void frmWebServer::changeTimer()
{
timer->setInterval(App::IntervalWebServer);
if (App::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("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 frmWebServer::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 frmWebServer::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 frmWebServer::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 frmWebServer::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 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();
App::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_btnClose_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());
}
}
}

View File

@@ -0,0 +1,47 @@
#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();
private:
Ui::frmWebServer *ui;
bool isOk;
WebServer *server;
QTimer *timer;
private slots:
void initForm();
void initConfig();
void saveConfig();
void changeTimer();
void append(int type, const QString &data, bool clear = false);
private slots:
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 // FRMWEBSERVER_H

View File

@@ -0,0 +1,251 @@
<?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="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

@@ -4,6 +4,7 @@
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#include <QtWebSockets>
#endif
#include "app.h"

View File

@@ -4,9 +4,9 @@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/main.ico"));
a.setWindowIcon(QIcon(":/main.ico"));
//设置编码以及加载中文翻译文件
//设置编码+字体+中文翻译文件
QUIHelper::setCode();
QUIHelper::setFont(":/DroidSansFallback.ttf");
QUIHelper::setTranslator(":/qt_zh_CN.qm");
@@ -20,8 +20,9 @@ int main(int argc, char *argv[])
App::readDeviceData();
frmMain w;
w.setWindowTitle(QString("网络调试助手V2020 本机IP: %1 QQ: 517216493").arg(QUIHelper::getLocalIP()));
w.show();
w.setWindowTitle("网络调试助手 V2021 (QQ: 517216493 WX: feiyangqingyun)");
QUIHelper::setFormInCenter(&w);
w.show();
return a.exec();
}

View File

@@ -6,12 +6,23 @@
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets
#判断是否有websocket模块
qtHaveModule(websockets) {
QT += websockets
DEFINES += websocket
}}
TARGET = nettool
TEMPLATE = app
DESTDIR = $$PWD/../bin
RC_FILE = other/main.rc
wasm {
DEFINES += emsdk
RESOURCES += other/font.qrc
} else {
DESTDIR = $$PWD/../bin
}
SOURCES += main.cpp
HEADERS += head.h

Binary file not shown.

5
nettool/other/font.qrc Normal file
View File

@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>DroidSansFallback.ttf</file>
</qresource>
</RCC>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB