diff --git a/comtool/api/app.cpp b/comtool/api/app.cpp index 062332e..caf1892 100644 --- a/comtool/api/app.cpp +++ b/comtool/api/app.cpp @@ -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() diff --git a/comtool/api/app.h b/comtool/api/app.h index 22c3840..499ffd0 100644 --- a/comtool/api/app.h +++ b/comtool/api/app.h @@ -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 diff --git a/comtool/form/frmcomtool.ui b/comtool/form/frmcomtool.ui index 4b6fe20..bf1f20a 100644 --- a/comtool/form/frmcomtool.ui +++ b/comtool/form/frmcomtool.ui @@ -11,6 +11,18 @@ + + 6 + + + 6 + + + 6 + + + 6 + @@ -32,9 +44,15 @@ + + + 180 + 0 + + - 200 + 180 16777215 @@ -60,6 +78,18 @@ QFrame::Sunken + + 6 + + + 6 + + + 6 + + + 6 + @@ -168,6 +198,18 @@ 串口配置 + + 6 + + + 6 + + + 6 + + + 6 + @@ -296,6 +338,18 @@ 网络配置 + + 6 + + + 6 + + + 6 + + + 6 + diff --git a/comtool/main.cpp b/comtool/main.cpp index 1905cf4..3875a2c 100644 --- a/comtool/main.cpp +++ b/comtool/main.cpp @@ -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(); diff --git a/nettool/api/api.pri b/nettool/api/api.pri index 5f363c7..8fbfe77 100644 --- a/nettool/api/api.pri +++ b/nettool/api/api.pri @@ -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 +} diff --git a/nettool/api/app.cpp b/nettool/api/app.cpp index 64f82af..1744e2a 100644 --- a/nettool/api/app.cpp +++ b/nettool/api/app.cpp @@ -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(); + } +} diff --git a/nettool/api/app.h b/nettool/api/app.h index 1094a6c..f2672cf 100644 --- a/nettool/api/app.h +++ b/nettool/api/app.h @@ -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 diff --git a/nettool/api/tcpclient.cpp b/nettool/api/tcpclient.cpp new file mode 100644 index 0000000..99bc130 --- /dev/null +++ b/nettool/api/tcpclient.cpp @@ -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(); +} diff --git a/nettool/api/tcpclient.h b/nettool/api/tcpclient.h new file mode 100644 index 0000000..7e77fad --- /dev/null +++ b/nettool/api/tcpclient.h @@ -0,0 +1,42 @@ +#ifndef TCPCLIENT_H +#define TCPCLIENT_H + +#include + +//为了方便并发和单独处理数据所以单独写个类专门处理 +#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 diff --git a/nettool/api/tcpserver.cpp b/nettool/api/tcpserver.cpp index a526995..0ba9472 100644 --- a/nettool/api/tcpserver.cpp +++ b/nettool/api/tcpserver.cpp @@ -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(); } } diff --git a/nettool/api/tcpserver.h b/nettool/api/tcpserver.h index 0c5327d..3758712 100644 --- a/nettool/api/tcpserver.h +++ b/nettool/api/tcpserver.h @@ -1,36 +1,7 @@ #ifndef TCPSERVER_H #define TCPSERVER_H -#include - -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 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 diff --git a/nettool/api/webclient.cpp b/nettool/api/webclient.cpp new file mode 100644 index 0000000..3c4bba4 --- /dev/null +++ b/nettool/api/webclient.cpp @@ -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(); +} diff --git a/nettool/api/webclient.h b/nettool/api/webclient.h new file mode 100644 index 0000000..7f9ae20 --- /dev/null +++ b/nettool/api/webclient.h @@ -0,0 +1,45 @@ +#ifndef WEBCLIENT_H +#define WEBCLIENT_H + +#include + +//为了方便并发和单独处理数据所以单独写个类专门处理 +#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 diff --git a/nettool/api/webserver.cpp b/nettool/api/webserver.cpp new file mode 100644 index 0000000..7c470eb --- /dev/null +++ b/nettool/api/webserver.cpp @@ -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(); + } +} diff --git a/nettool/api/webserver.h b/nettool/api/webserver.h new file mode 100644 index 0000000..bc60f4e --- /dev/null +++ b/nettool/api/webserver.h @@ -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 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 diff --git a/nettool/form/form.pri b/nettool/form/form.pri index b5f01bb..fe4386d 100644 --- a/nettool/form/form.pri +++ b/nettool/form/form.pri @@ -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 +} diff --git a/nettool/form/frmmain.cpp b/nettool/form/frmmain.cpp index f1fc67d..78504e3 100644 --- a/nettool/form/frmmain.cpp +++ b/nettool/form/frmmain.cpp @@ -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(); } diff --git a/nettool/form/frmmain.h b/nettool/form/frmmain.h index ea5effd..55dc775 100644 --- a/nettool/form/frmmain.h +++ b/nettool/form/frmmain.h @@ -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 diff --git a/nettool/form/frmmain.ui b/nettool/form/frmmain.ui index 8da74e2..d6f91ef 100644 --- a/nettool/form/frmmain.ui +++ b/nettool/form/frmmain.ui @@ -35,58 +35,12 @@ QTabWidget::South - 2 + -1 - - - TCP客户端 - - - - - TCP服务器 - - - - - UDP客户端 - - - - - UDP服务器 - - - - - frmTcpClient - QWidget -
frmtcpclient.h
- 1 -
- - frmTcpServer - QWidget -
frmtcpserver.h
- 1 -
- - frmUdpServer - QWidget -
frmudpserver.h
- 1 -
- - frmUdpClient - QWidget -
frmudpclient.h
- 1 -
-
diff --git a/nettool/form/frmtcpclient.cpp b/nettool/form/frmtcpclient.cpp index a81cd65..510cebc 100644 --- a/nettool/form/frmtcpclient.cpp +++ b/nettool/form/frmtcpclient.cpp @@ -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(); } diff --git a/nettool/form/frmtcpclient.h b/nettool/form/frmtcpclient.h index c8b5fec..31efd8f 100644 --- a/nettool/form/frmtcpclient.h +++ b/nettool/form/frmtcpclient.h @@ -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(); diff --git a/nettool/form/frmtcpclient.ui b/nettool/form/frmtcpclient.ui index a54a6a6..0c4cd9f 100644 --- a/nettool/form/frmtcpclient.ui +++ b/nettool/form/frmtcpclient.ui @@ -14,6 +14,18 @@ Form + + 6 + + + 6 + + + 6 + + + 6 + @@ -43,16 +55,16 @@ - 9 + 6 - 9 + 6 - 9 + 6 - 9 + 6 @@ -71,7 +83,7 @@ - Ascii控制字符 + 控制字符 diff --git a/nettool/form/frmtcpserver.cpp b/nettool/form/frmtcpserver.cpp index 25fa421..db8875f 100644 --- a/nettool/form/frmtcpserver.cpp +++ b/nettool/form/frmtcpserver.cpp @@ -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()); } } } diff --git a/nettool/form/frmtcpserver.h b/nettool/form/frmtcpserver.h index e43a544..77174e9 100644 --- a/nettool/form/frmtcpserver.h +++ b/nettool/form/frmtcpserver.h @@ -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(); diff --git a/nettool/form/frmtcpserver.ui b/nettool/form/frmtcpserver.ui index beb7d7a..48ea61f 100644 --- a/nettool/form/frmtcpserver.ui +++ b/nettool/form/frmtcpserver.ui @@ -14,6 +14,18 @@ Form + + 6 + + + 6 + + + 6 + + + 6 + @@ -43,16 +55,16 @@ - 9 + 6 - 9 + 6 - 9 + 6 - 9 + 6 @@ -71,7 +83,7 @@ - Ascii控制字符 + 控制字符 diff --git a/nettool/form/frmudpclient.cpp b/nettool/form/frmudpclient.cpp index 66dc84c..5bc38f2 100644 --- a/nettool/form/frmudpclient.cpp +++ b/nettool/form/frmudpclient.cpp @@ -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(); } diff --git a/nettool/form/frmudpclient.h b/nettool/form/frmudpclient.h index 8f468fd..762607f 100644 --- a/nettool/form/frmudpclient.h +++ b/nettool/form/frmudpclient.h @@ -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); diff --git a/nettool/form/frmudpclient.ui b/nettool/form/frmudpclient.ui index fd04640..70d430c 100644 --- a/nettool/form/frmudpclient.ui +++ b/nettool/form/frmudpclient.ui @@ -14,6 +14,18 @@ Form + + 6 + + + 6 + + + 6 + + + 6 + @@ -42,6 +54,18 @@ QFrame::Sunken + + 6 + + + 6 + + + 6 + + + 6 + @@ -59,7 +83,7 @@ - Ascii控制字符 + 控制字符 diff --git a/nettool/form/frmudpserver.cpp b/nettool/form/frmudpserver.cpp index ac3d914..f337d5e 100644 --- a/nettool/form/frmudpserver.cpp +++ b/nettool/form/frmudpserver.cpp @@ -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(); } diff --git a/nettool/form/frmudpserver.h b/nettool/form/frmudpserver.h index d65b505..436a779 100644 --- a/nettool/form/frmudpserver.h +++ b/nettool/form/frmudpserver.h @@ -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); diff --git a/nettool/form/frmudpserver.ui b/nettool/form/frmudpserver.ui index 13746ec..25f8279 100644 --- a/nettool/form/frmudpserver.ui +++ b/nettool/form/frmudpserver.ui @@ -14,6 +14,18 @@ Form + + 6 + + + 6 + + + 6 + + + 6 + @@ -42,6 +54,18 @@ QFrame::Sunken + + 6 + + + 6 + + + 6 + + + 6 + @@ -59,7 +83,7 @@ - Ascii控制字符 + 控制字符 diff --git a/nettool/form/frmwebclient.cpp b/nettool/form/frmwebclient.cpp new file mode 100644 index 0000000..e244175 --- /dev/null +++ b/nettool/form/frmwebclient.cpp @@ -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); +} diff --git a/nettool/form/frmwebclient.h b/nettool/form/frmwebclient.h new file mode 100644 index 0000000..ee6f9a4 --- /dev/null +++ b/nettool/form/frmwebclient.h @@ -0,0 +1,50 @@ +#ifndef FRMWEBCLIENT_H +#define FRMWEBCLIENT_H + +#include +#include + +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 diff --git a/nettool/form/frmwebclient.ui b/nettool/form/frmwebclient.ui new file mode 100644 index 0000000..6ce3ab0 --- /dev/null +++ b/nettool/form/frmwebclient.ui @@ -0,0 +1,225 @@ + + + frmWebClient + + + + 0 + 0 + 800 + 600 + + + + Form + + + + 6 + + + 6 + + + 6 + + + 6 + + + + + true + + + + + + + + 170 + 0 + + + + + 170 + 16777215 + + + + QFrame::Box + + + QFrame::Sunken + + + + 6 + + + 6 + + + 6 + + + 6 + + + + + 16进制接收 + + + + + + + 16进制发送 + + + + + + + 文本字符 + + + + + + + 暂停显示 + + + + + + + 模拟设备 + + + + + + + 定时发送 + + + + + + + + + + 服务器地址 + + + + + + + + + + 服务器端口 + + + + + + + + + + 连接 + + + + + + + 保存 + + + + + + + 清空 + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + true + + + + + + + + 80 + 0 + + + + + 80 + 16777215 + + + + 发送 + + + + + + + + + + + diff --git a/nettool/form/frmwebserver.cpp b/nettool/form/frmwebserver.cpp new file mode 100644 index 0000000..25f194d --- /dev/null +++ b/nettool/form/frmwebserver.cpp @@ -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()); + } + } +} diff --git a/nettool/form/frmwebserver.h b/nettool/form/frmwebserver.h new file mode 100644 index 0000000..9048bd8 --- /dev/null +++ b/nettool/form/frmwebserver.h @@ -0,0 +1,47 @@ +#ifndef FRMWEBSERVER_H +#define FRMWEBSERVER_H + +#include +#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 diff --git a/nettool/form/frmwebserver.ui b/nettool/form/frmwebserver.ui new file mode 100644 index 0000000..964ad6e --- /dev/null +++ b/nettool/form/frmwebserver.ui @@ -0,0 +1,251 @@ + + + frmWebServer + + + + 0 + 0 + 800 + 600 + + + + Form + + + + 6 + + + 6 + + + 6 + + + 6 + + + + + true + + + + + + + + 170 + 0 + + + + + 170 + 16777215 + + + + QFrame::Box + + + QFrame::Sunken + + + + 6 + + + 6 + + + 6 + + + 6 + + + + + 16进制接收 + + + + + + + 16进制发送 + + + + + + + 文本字符 + + + + + + + 暂停显示 + + + + + + + 模拟设备 + + + + + + + 定时发送 + + + + + + + + + + 监听地址 + + + + + + + + + + 监听端口 + + + + + + + + + + 监听 + + + + + + + 保存 + + + + + + + 清空 + + + + + + + 断开 + + + + + + + + 0 + 25 + + + + QFrame::Box + + + QFrame::Sunken + + + 共 0 个客户端 + + + Qt::AlignCenter + + + + + + + + + + 对所有客户端发送 + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + true + + + + + + + + 80 + 0 + + + + + 80 + 16777215 + + + + 发送 + + + + + + + + + + + diff --git a/nettool/head.h b/nettool/head.h index 634b2a2..3b1b075 100644 --- a/nettool/head.h +++ b/nettool/head.h @@ -4,6 +4,7 @@ #if (QT_VERSION > QT_VERSION_CHECK(5,0,0)) #include +#include #endif #include "app.h" diff --git a/nettool/main.cpp b/nettool/main.cpp index d45b283..5ddbf70 100644 --- a/nettool/main.cpp +++ b/nettool/main.cpp @@ -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(); } diff --git a/nettool/nettool.pro b/nettool/nettool.pro index b41c97f..cd44241 100644 --- a/nettool/nettool.pro +++ b/nettool/nettool.pro @@ -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 diff --git a/nettool/other/DroidSansFallback.ttf b/nettool/other/DroidSansFallback.ttf new file mode 100644 index 0000000..8c6fabe Binary files /dev/null and b/nettool/other/DroidSansFallback.ttf differ diff --git a/nettool/other/font.qrc b/nettool/other/font.qrc new file mode 100644 index 0000000..d28c6d2 --- /dev/null +++ b/nettool/other/font.qrc @@ -0,0 +1,5 @@ + + + DroidSansFallback.ttf + + diff --git a/nettool/snap/QQ截图20180514145725.jpg b/nettool/snap/QQ截图20180514145725.jpg deleted file mode 100644 index 6d7fa5e..0000000 Binary files a/nettool/snap/QQ截图20180514145725.jpg and /dev/null differ diff --git a/nettool/snap/QQ截图20180514145729.jpg b/nettool/snap/QQ截图20180514145729.jpg deleted file mode 100644 index 4a408fe..0000000 Binary files a/nettool/snap/QQ截图20180514145729.jpg and /dev/null differ diff --git a/nettool/snap/QQ截图20180514145732.jpg b/nettool/snap/QQ截图20180514145732.jpg deleted file mode 100644 index 0258092..0000000 Binary files a/nettool/snap/QQ截图20180514145732.jpg and /dev/null differ diff --git a/nettool/snap/QQ截图20180514145909.jpg b/nettool/snap/QQ截图20180514145909.jpg deleted file mode 100644 index 39d0cbe..0000000 Binary files a/nettool/snap/QQ截图20180514145909.jpg and /dev/null differ