diff --git a/nettool/api/tcpclient.cpp b/nettool/api/tcpclient.cpp index ba39afd..d1f022a 100644 --- a/nettool/api/tcpclient.cpp +++ b/nettool/api/tcpclient.cpp @@ -8,13 +8,13 @@ TcpClient::TcpClient(QTcpSocket *socket, QObject *parent) : QObject(parent) ip = ip.replace("::ffff:", ""); port = socket->peerPort(); + connect(socket, SIGNAL(disconnected()), this, SLOT(slot_disconnected())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) - connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)),this, SLOT(disconnected())); + connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slot_error())); #else - connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected())); + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_error())); #endif - connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); - connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); + connect(socket, SIGNAL(readyRead()), this, SLOT(slot_readData())); } QString TcpClient::getIP() const @@ -27,14 +27,19 @@ int TcpClient::getPort() const return this->port; } -void TcpClient::disconnected() +void TcpClient::slot_disconnected() { + emit disconnected(ip, port); socket->deleteLater(); this->deleteLater(); - emit clientDisconnected(); } -void TcpClient::readData() +void TcpClient::slot_error() +{ + emit error(ip, port, socket->errorString()); +} + +void TcpClient::slot_readData() { QByteArray data = socket->readAll(); if (data.length() <= 0) { @@ -79,6 +84,11 @@ void TcpClient::sendData(const QString &data) emit sendData(ip, port, data); } +void TcpClient::disconnectFromHost() +{ + socket->disconnectFromHost(); +} + void TcpClient::abort() { socket->abort(); diff --git a/nettool/api/tcpclient.h b/nettool/api/tcpclient.h index 7e77fad..963e16a 100644 --- a/nettool/api/tcpclient.h +++ b/nettool/api/tcpclient.h @@ -26,16 +26,20 @@ public: int getPort() const; private slots: - void disconnected(); - void readData(); + void slot_disconnected(); + void slot_error(); + void slot_readData(); signals: - void clientDisconnected(); + void disconnected(const QString &ip, int port); + void error(const QString &ip, int port, const QString &error); + 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 disconnectFromHost(); void abort(); }; diff --git a/nettool/api/tcpserver.cpp b/nettool/api/tcpserver.cpp index 5522900..0fbcdaa 100644 --- a/nettool/api/tcpserver.cpp +++ b/nettool/api/tcpserver.cpp @@ -3,34 +3,27 @@ TcpServer::TcpServer(QObject *parent) : QTcpServer(parent) { - connect(this, SIGNAL(newConnection()), this, SLOT(newConnection())); + connect(this, SIGNAL(newConnection()), this, SLOT(slot_newConnection())); } -void TcpServer::newConnection() +void TcpServer::slot_newConnection() { QTcpSocket *socket = this->nextPendingConnection(); TcpClient *client = new TcpClient(socket, this); - connect(client, SIGNAL(clientDisconnected()), this, SLOT(disconnected())); + connect(client, SIGNAL(disconnected(QString, int)), this, SLOT(slot_disconnected(QString, int))); + connect(client, SIGNAL(error(QString, int, QString)), this, SIGNAL(error(QString, int, QString))); 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, "客户端上线"); - + emit connected(client->getIP(), client->getPort()); //连接后加入链表 clients.append(client); } -void TcpServer::disconnected() +void TcpServer::slot_disconnected(const QString &ip, int port) { TcpClient *client = (TcpClient *)sender(); - QString ip = client->getIP(); - int port = client->getPort(); - emit clientDisconnected(ip, port); - emit sendData(ip, port, "客户端下线"); - + emit disconnected(ip, port); //断开连接后从链表中移除 clients.removeOne(client); } @@ -44,7 +37,6 @@ bool TcpServer::start() void TcpServer::stop() { remove(); - this->disconnected(); this->close(); } diff --git a/nettool/api/tcpserver.h b/nettool/api/tcpserver.h index 3758712..f289c69 100644 --- a/nettool/api/tcpserver.h +++ b/nettool/api/tcpserver.h @@ -13,16 +13,17 @@ private: QList clients; private slots: - void newConnection(); - void disconnected(); + void slot_newConnection(); + void slot_disconnected(const QString &ip, int port); signals: + void connected(const QString &ip, int port); + void disconnected(const QString &ip, int port); + void error(const QString &ip, int port, const QString &error); + 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(); diff --git a/nettool/api/webclient.cpp b/nettool/api/webclient.cpp index 93c5a5d..43d18b3 100644 --- a/nettool/api/webclient.cpp +++ b/nettool/api/webclient.cpp @@ -8,10 +8,10 @@ WebClient::WebClient(QWebSocket *socket, QObject *parent) : QObject(parent) 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(disconnected()), this, SLOT(slot_disconnected())); + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_error())); - //暂时使用前面两个信号,部分系统上后面两个信号Qt没实现,目前测试到5.15.2 + //暂时使用前面两个信号,部分系统后面两个信号Qt没实现,目前测试到5.15.2 //在win上如果两组信号都关联了则都会触发,另外一组信号就是多个参数表示是否是最后一个数据包 connect(socket, SIGNAL(textMessageReceived(QString)), this, SLOT(textMessageReceived(QString))); connect(socket, SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(binaryMessageReceived(QByteArray))); @@ -29,11 +29,16 @@ int WebClient::getPort() const return this->port; } -void WebClient::disconnected() +void WebClient::slot_disconnected() { + emit disconnected(ip, port); socket->deleteLater(); this->deleteLater(); - emit clientDisconnected(); +} + +void WebClient::slot_error() +{ + emit error(ip, port, socket->errorString()); } void WebClient::textFrameReceived(const QString &data, bool isLastFrame) diff --git a/nettool/api/webclient.h b/nettool/api/webclient.h index 7f9ae20..2ea7523 100644 --- a/nettool/api/webclient.h +++ b/nettool/api/webclient.h @@ -26,14 +26,18 @@ public: int getPort() const; private slots: - void disconnected(); + void slot_disconnected(); + void slot_error(); + 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 disconnected(const QString &ip, int port); + void error(const QString &ip, int port, const QString &error); + void sendData(const QString &ip, int port, const QString &data); void receiveData(const QString &ip, int port, const QString &data); diff --git a/nettool/api/webserver.cpp b/nettool/api/webserver.cpp index b3c9b7b..4d8263a 100644 --- a/nettool/api/webserver.cpp +++ b/nettool/api/webserver.cpp @@ -3,34 +3,27 @@ WebServer::WebServer(const QString &serverName, SslMode secureMode, QObject *parent) : QWebSocketServer(serverName, secureMode, parent) { - connect(this, SIGNAL(newConnection()), this, SLOT(newConnection())); + connect(this, SIGNAL(newConnection()), this, SLOT(slot_newConnection())); } -void WebServer::newConnection() +void WebServer::slot_newConnection() { QWebSocket *socket = this->nextPendingConnection(); WebClient *client = new WebClient(socket, this); - connect(client, SIGNAL(clientDisconnected()), this, SLOT(disconnected())); + connect(client, SIGNAL(disconnected(QString, int)), this, SLOT(slot_disconnected(QString, int))); + connect(client, SIGNAL(error(QString, int, QString)), this, SIGNAL(error(QString, int, QString))); 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, "客户端上线"); - + emit connected(client->getIP(), client->getPort()); //连接后加入链表 clients.append(client); } -void WebServer::disconnected() +void WebServer::slot_disconnected(const QString &ip, int port) { WebClient *client = (WebClient *)sender(); - QString ip = client->getIP(); - int port = client->getPort(); - emit clientDisconnected(ip, port); - emit sendData(ip, port, "客户端下线"); - + emit disconnected(ip, port); //断开连接后从链表中移除 clients.removeOne(client); } diff --git a/nettool/api/webserver.h b/nettool/api/webserver.h index bc60f4e..a30c728 100644 --- a/nettool/api/webserver.h +++ b/nettool/api/webserver.h @@ -13,15 +13,16 @@ private: QList clients; private slots: - void newConnection(); - void disconnected(); + void slot_newConnection(); + void slot_disconnected(const QString &ip, int port); signals: - void sendData(const QString &ip, int port, const QString &data); - void receiveData(const QString &ip, int port, const QString &data); + void connected(const QString &ip, int port); + void disconnected(const QString &ip, int port); + void error(const QString &ip, int port, const QString &error); - 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); public slots: //启动服务 diff --git a/nettool/form/frmtcpclient.cpp b/nettool/form/frmtcpclient.cpp index f064722..36b5cc7 100644 --- a/nettool/form/frmtcpclient.cpp +++ b/nettool/form/frmtcpclient.cpp @@ -14,19 +14,36 @@ frmTcpClient::~frmTcpClient() delete ui; } +bool frmTcpClient::eventFilter(QObject *watched, QEvent *event) +{ + //双击清空 + if (watched == ui->txtMain->viewport()) { + if (event->type() == QEvent::MouseButtonDblClick) { + on_btnClear_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + void frmTcpClient::initForm() { + QFont font; + font.setPixelSize(16); + ui->txtMain->setFont(font); + ui->txtMain->viewport()->installEventFilter(this); + isOk = false; //实例化对象并绑定信号槽 socket = new QTcpSocket(this); connect(socket, SIGNAL(connected()), this, SLOT(connected())); -#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) - connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(disconnected())); -#else - connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected())); -#endif connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); +#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) + connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error())); +#else + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); +#endif connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); //定时器发送数据 @@ -146,10 +163,10 @@ void frmTcpClient::append(int type, const QString &data, bool clear) ui->txtMain->setTextColor(QColor("#22A3A9")); } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("#D64D54")); + ui->txtMain->setTextColor(QColor("#753775")); } else { - strType = "信息"; - ui->txtMain->setTextColor(QColor("#A279C5")); + strType = "错误"; + ui->txtMain->setTextColor(QColor("#D64D54")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -162,20 +179,20 @@ void frmTcpClient::connected() isOk = true; ui->btnConnect->setText("断开"); append(0, "服务器连接"); - append(2, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort())); - append(2, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort())); + append(0, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort())); + append(0, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort())); } void frmTcpClient::disconnected() { isOk = false; - //socket->abort(); ui->btnConnect->setText("连接"); append(1, "服务器断开"); - //打印下可能的错误信息 - if (socket->error() != QTcpSocket::UnknownSocketError) { - append(2, socket->errorString()); - } +} + +void frmTcpClient::error() +{ + append(2, socket->errorString()); } void frmTcpClient::readData() @@ -227,9 +244,10 @@ void frmTcpClient::on_btnConnect_clicked() { if (ui->btnConnect->text() == "连接") { //断开所有连接和操作 - socket->abort(); + //socket->abort(); //绑定网卡和端口 - //有个后遗症,关闭连接或者关闭程序后还会保持几分钟导致不能重复绑定 + //有个后遗症,客户端这边断开连接后还会保持几分钟导致不能重复绑定 + //如果是服务器断开则可以继续使用 //提示 The bound address is already in use //参考 https://www.cnblogs.com/baiduboy/p/7426822.html #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0)) diff --git a/nettool/form/frmtcpclient.h b/nettool/form/frmtcpclient.h index 66c88ed..2a1604b 100644 --- a/nettool/form/frmtcpclient.h +++ b/nettool/form/frmtcpclient.h @@ -16,6 +16,9 @@ public: explicit frmTcpClient(QWidget *parent = 0); ~frmTcpClient(); +protected: + bool eventFilter(QObject *watched, QEvent *event); + private: Ui::frmTcpClient *ui; @@ -33,6 +36,7 @@ private slots: private slots: void connected(); void disconnected(); + void error(); void readData(); void sendData(const QString &data); diff --git a/nettool/form/frmtcpserver.cpp b/nettool/form/frmtcpserver.cpp index e016d1e..421ad99 100644 --- a/nettool/form/frmtcpserver.cpp +++ b/nettool/form/frmtcpserver.cpp @@ -12,17 +12,37 @@ frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp frmTcpServer::~frmTcpServer() { + //结束的时候停止服务 + server->stop(); delete ui; } +bool frmTcpServer::eventFilter(QObject *watched, QEvent *event) +{ + //双击清空 + if (watched == ui->txtMain->viewport()) { + if (event->type() == QEvent::MouseButtonDblClick) { + on_btnClear_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + void frmTcpServer::initForm() { + QFont font; + font.setPixelSize(16); + ui->txtMain->setFont(font); + ui->txtMain->viewport()->installEventFilter(this); + isOk = false; //实例化对象并绑定信号槽 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(connected(QString, int)), this, SLOT(connected(QString, int))); + connect(server, SIGNAL(disconnected(QString, int)), this, SLOT(disconnected(QString, int))); + connect(server, SIGNAL(error(QString, int, QString)), this, SLOT(error(QString, int, QString))); 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))); @@ -133,10 +153,10 @@ void frmTcpServer::append(int type, const QString &data, bool clear) ui->txtMain->setTextColor(QColor("#22A3A9")); } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("#D64D54")); + ui->txtMain->setTextColor(QColor("#753775")); } else { - strType = "信息"; - ui->txtMain->setTextColor(QColor("#A279C5")); + strType = "错误"; + ui->txtMain->setTextColor(QColor("#D64D54")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -144,15 +164,19 @@ void frmTcpServer::append(int type, const QString &data, bool clear) currentCount++; } -void frmTcpServer::clientConnected(const QString &ip, int port) +void frmTcpServer::connected(const QString &ip, int port) { + append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端上线")); + QString str = QString("%1:%2").arg(ip).arg(port); ui->listWidget->addItem(str); ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count())); } -void frmTcpServer::clientDisconnected(const QString &ip, int port) +void frmTcpServer::disconnected(const QString &ip, int port) { + append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端下线")); + int row = -1; QString str = QString("%1:%2").arg(ip).arg(port); for (int i = 0; i < ui->listWidget->count(); i++) { @@ -166,17 +190,19 @@ void frmTcpServer::clientDisconnected(const QString &ip, int port) ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count())); } +void frmTcpServer::error(const QString &ip, int port, const QString &error) +{ + append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg(error)); +} + void frmTcpServer::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); + append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg(data)); } void frmTcpServer::receiveData(const QString &ip, int port, const QString &data) { - QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(data); - append(1, str); + append(1, QString("[%1:%2] %3").arg(ip).arg(port).arg(data)); } void frmTcpServer::on_btnListen_clicked() @@ -229,7 +255,7 @@ void frmTcpServer::on_btnSend_clicked() } } -void frmTcpServer::on_btnClose_clicked() +void frmTcpServer::on_btnRemove_clicked() { if (ui->ckSelectAll->isChecked()) { server->remove(); diff --git a/nettool/form/frmtcpserver.h b/nettool/form/frmtcpserver.h index 49b51cf..276e856 100644 --- a/nettool/form/frmtcpserver.h +++ b/nettool/form/frmtcpserver.h @@ -16,6 +16,9 @@ public: explicit frmTcpServer(QWidget *parent = 0); ~frmTcpServer(); +protected: + bool eventFilter(QObject *watched, QEvent *event); + private: Ui::frmTcpServer *ui; @@ -31,8 +34,10 @@ private slots: 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 connected(const QString &ip, int port); + void disconnected(const QString &ip, int port); + void error(const QString &ip, int port, const QString &error); + void sendData(const QString &ip, int port, const QString &data); void receiveData(const QString &ip, int port, const QString &data); @@ -41,7 +46,7 @@ private slots: void on_btnSave_clicked(); void on_btnClear_clicked(); void on_btnSend_clicked(); - void on_btnClose_clicked(); + void on_btnRemove_clicked(); }; #endif // FRMTCPSERVER_H diff --git a/nettool/form/frmtcpserver.ui b/nettool/form/frmtcpserver.ui index 1f8d387..cc9bdb5 100644 --- a/nettool/form/frmtcpserver.ui +++ b/nettool/form/frmtcpserver.ui @@ -153,9 +153,9 @@ - + - 断开 + 移除 @@ -262,7 +262,7 @@ btnListen btnSave btnClear - btnClose + btnRemove listWidget ckSelectAll diff --git a/nettool/form/frmudpclient.cpp b/nettool/form/frmudpclient.cpp index 8a4bd06..9835529 100644 --- a/nettool/form/frmudpclient.cpp +++ b/nettool/form/frmudpclient.cpp @@ -14,10 +14,32 @@ frmUdpClient::~frmUdpClient() delete ui; } +bool frmUdpClient::eventFilter(QObject *watched, QEvent *event) +{ + //双击清空 + if (watched == ui->txtMain->viewport()) { + if (event->type() == QEvent::MouseButtonDblClick) { + on_btnClear_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + void frmUdpClient::initForm() { + QFont font; + font.setPixelSize(16); + ui->txtMain->setFont(font); + ui->txtMain->viewport()->installEventFilter(this); + //实例化对象并绑定信号槽 socket = new QUdpSocket(this); +#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) + connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error())); +#else + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); +#endif connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); //定时器发送数据 @@ -131,10 +153,10 @@ void frmUdpClient::append(int type, const QString &data, bool clear) ui->txtMain->setTextColor(QColor("#22A3A9")); } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("#D64D54")); + ui->txtMain->setTextColor(QColor("#753775")); } else { - strType = "信息"; - ui->txtMain->setTextColor(QColor("#A279C5")); + strType = "错误"; + ui->txtMain->setTextColor(QColor("#D64D54")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -142,6 +164,11 @@ void frmUdpClient::append(int type, const QString &data, bool clear) currentCount++; } +void frmUdpClient::error() +{ + append(2, socket->errorString()); +} + void frmUdpClient::readData() { QHostAddress host; diff --git a/nettool/form/frmudpclient.h b/nettool/form/frmudpclient.h index 89c85b0..072b714 100644 --- a/nettool/form/frmudpclient.h +++ b/nettool/form/frmudpclient.h @@ -16,6 +16,9 @@ public: explicit frmUdpClient(QWidget *parent = 0); ~frmUdpClient(); +protected: + bool eventFilter(QObject *watched, QEvent *event); + private: Ui::frmUdpClient *ui; @@ -30,6 +33,7 @@ private slots: void append(int type, const QString &data, bool clear = false); private slots: + void error(); void readData(); void sendData(const QString &ip, int port, const QString &data); diff --git a/nettool/form/frmudpserver.cpp b/nettool/form/frmudpserver.cpp index 4b80db4..c9d2a20 100644 --- a/nettool/form/frmudpserver.cpp +++ b/nettool/form/frmudpserver.cpp @@ -15,10 +15,32 @@ frmUdpServer::~frmUdpServer() delete ui; } +bool frmUdpServer::eventFilter(QObject *watched, QEvent *event) +{ + //双击清空 + if (watched == ui->txtMain->viewport()) { + if (event->type() == QEvent::MouseButtonDblClick) { + on_btnClear_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + void frmUdpServer::initForm() { + QFont font; + font.setPixelSize(16); + ui->txtMain->setFont(font); + ui->txtMain->viewport()->installEventFilter(this); + //实例化对象并绑定信号槽 socket = new QUdpSocket(this); +#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) + connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error())); +#else + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); +#endif connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); //定时器发送数据 @@ -128,10 +150,10 @@ void frmUdpServer::append(int type, const QString &data, bool clear) ui->txtMain->setTextColor(QColor("#22A3A9")); } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("#D64D54")); + ui->txtMain->setTextColor(QColor("#753775")); } else { - strType = "信息"; - ui->txtMain->setTextColor(QColor("#A279C5")); + strType = "错误"; + ui->txtMain->setTextColor(QColor("#D64D54")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -139,6 +161,11 @@ void frmUdpServer::append(int type, const QString &data, bool clear) currentCount++; } +void frmUdpServer::error() +{ + append(2, socket->errorString()); +} + void frmUdpServer::readData() { QHostAddress host; @@ -166,7 +193,19 @@ void frmUdpServer::readData() QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer); append(1, str); - clientConnected(ip, port); + + //先过滤重复的 + str = QString("%1:%2").arg(ip).arg(port); + for (int i = 0; i < ui->listWidget->count(); i++) { + QString s = ui->listWidget->item(i)->text(); + if (str == s) { + return; + } + } + + //添加到列表 + ui->listWidget->addItem(str); + ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count())); if (AppConfig::DebugUdpServer) { int count = AppData::Keys.count(); @@ -197,21 +236,6 @@ void frmUdpServer::sendData(const QString &ip, int port, const QString &data) append(0, str); } -void frmUdpServer::clientConnected(const QString &ip, int port) -{ - //先过滤重复的 - QString str = QString("%1:%2").arg(ip).arg(port); - for (int i = 0; i < ui->listWidget->count(); i++) { - QString s = ui->listWidget->item(i)->text(); - if (str == s) { - return; - } - } - - ui->listWidget->addItem(str); - ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count())); -} - void frmUdpServer::on_btnListen_clicked() { if (ui->btnListen->text() == "监听") { @@ -260,3 +284,15 @@ void frmUdpServer::on_btnSend_clicked() } } } + +void frmUdpServer::on_btnRemove_clicked() +{ + if (ui->ckSelectAll->isChecked()) { + ui->listWidget->clear(); + } else { + int row = ui->listWidget->currentRow(); + if (row >= 0) { + delete ui->listWidget->takeItem(row); + } + } +} diff --git a/nettool/form/frmudpserver.h b/nettool/form/frmudpserver.h index 05e711b..c851b3c 100644 --- a/nettool/form/frmudpserver.h +++ b/nettool/form/frmudpserver.h @@ -16,6 +16,9 @@ public: explicit frmUdpServer(QWidget *parent = 0); ~frmUdpServer(); +protected: + bool eventFilter(QObject *watched, QEvent *event); + private: Ui::frmUdpServer *ui; @@ -30,15 +33,16 @@ private slots: void append(int type, const QString &data, bool clear = false); private slots: + void error(); void readData(); void sendData(const QString &ip, int port, const QString &data); - void clientConnected(const QString &ip, int port); private slots: void on_btnListen_clicked(); void on_btnSave_clicked(); void on_btnClear_clicked(); void on_btnSend_clicked(); + void on_btnRemove_clicked(); }; #endif // FRMUDPSERVER_H diff --git a/nettool/form/frmudpserver.ui b/nettool/form/frmudpserver.ui index 97468d2..c686cf2 100644 --- a/nettool/form/frmudpserver.ui +++ b/nettool/form/frmudpserver.ui @@ -152,6 +152,13 @@ + + + + 移除 + + + diff --git a/nettool/form/frmwebclient.cpp b/nettool/form/frmwebclient.cpp index 9bcb374..85737dc 100644 --- a/nettool/form/frmwebclient.cpp +++ b/nettool/form/frmwebclient.cpp @@ -14,15 +14,32 @@ frmWebClient::~frmWebClient() delete ui; } +bool frmWebClient::eventFilter(QObject *watched, QEvent *event) +{ + //双击清空 + if (watched == ui->txtMain->viewport()) { + if (event->type() == QEvent::MouseButtonDblClick) { + on_btnClear_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + void frmWebClient::initForm() { + QFont font; + font.setPixelSize(16); + ui->txtMain->setFont(font); + ui->txtMain->viewport()->installEventFilter(this); + 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())); + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); //暂时使用前面两个信号,部分系统上后面两个信号Qt没实现,目前测试到5.15.2 //在win上如果两组信号都关联了则都会触发,另外一组信号就是多个参数表示是否是最后一个数据包 @@ -133,10 +150,10 @@ void frmWebClient::append(int type, const QString &data, bool clear) ui->txtMain->setTextColor(QColor("#22A3A9")); } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("#D64D54")); + ui->txtMain->setTextColor(QColor("#753775")); } else { - strType = "信息"; - ui->txtMain->setTextColor(QColor("#A279C5")); + strType = "错误"; + ui->txtMain->setTextColor(QColor("#D64D54")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -149,20 +166,20 @@ void frmWebClient::connected() isOk = true; ui->btnConnect->setText("断开"); append(0, "服务器连接"); - append(2, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort())); - append(2, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort())); + append(0, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort())); + append(0, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort())); } void frmWebClient::disconnected() { isOk = false; - //socket->abort(); ui->btnConnect->setText("连接"); - append(1, "服务器断开"); - //打印下可能的错误信息 - if (socket->error() != QTcpSocket::UnknownSocketError) { - append(2, socket->errorString()); - } + append(1, "服务器断开"); +} + +void frmWebClient::error() +{ + append(2, socket->errorString()); } void frmWebClient::sendData(const QString &data) diff --git a/nettool/form/frmwebclient.h b/nettool/form/frmwebclient.h index 7533d2f..a6e617e 100644 --- a/nettool/form/frmwebclient.h +++ b/nettool/form/frmwebclient.h @@ -16,6 +16,9 @@ public: explicit frmWebClient(QWidget *parent = 0); ~frmWebClient(); +protected: + bool eventFilter(QObject *watched, QEvent *event); + private: Ui::frmWebClient *ui; @@ -33,6 +36,7 @@ private slots: private slots: void connected(); void disconnected(); + void error(); void sendData(const QString &data); void textFrameReceived(const QString &data, bool isLastFrame); diff --git a/nettool/form/frmwebserver.cpp b/nettool/form/frmwebserver.cpp index 84a957a..0ade9df 100644 --- a/nettool/form/frmwebserver.cpp +++ b/nettool/form/frmwebserver.cpp @@ -15,14 +15,32 @@ frmWebServer::~frmWebServer() delete ui; } +bool frmWebServer::eventFilter(QObject *watched, QEvent *event) +{ + //双击清空 + if (watched == ui->txtMain->viewport()) { + if (event->type() == QEvent::MouseButtonDblClick) { + on_btnClear_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + void frmWebServer::initForm() { + QFont font; + font.setPixelSize(16); + ui->txtMain->setFont(font); + ui->txtMain->viewport()->installEventFilter(this); + 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(connected(QString, int)), this, SLOT(connected(QString, int))); + connect(server, SIGNAL(disconnected(QString, int)), this, SLOT(disconnected(QString, int))); + connect(server, SIGNAL(error(QString, int, QString)), this, SLOT(error(QString, int, QString))); 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))); @@ -133,10 +151,10 @@ void frmWebServer::append(int type, const QString &data, bool clear) ui->txtMain->setTextColor(QColor("#22A3A9")); } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("#D64D54")); + ui->txtMain->setTextColor(QColor("#753775")); } else { - strType = "信息"; - ui->txtMain->setTextColor(QColor("#A279C5")); + strType = "错误"; + ui->txtMain->setTextColor(QColor("#D64D54")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -144,15 +162,19 @@ void frmWebServer::append(int type, const QString &data, bool clear) currentCount++; } -void frmWebServer::clientConnected(const QString &ip, int port) +void frmWebServer::connected(const QString &ip, int port) { + append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端上线")); + 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) +void frmWebServer::disconnected(const QString &ip, int port) { + append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端下线")); + int row = -1; QString str = QString("%1:%2").arg(ip).arg(port); for (int i = 0; i < ui->listWidget->count(); i++) { @@ -166,17 +188,19 @@ void frmWebServer::clientDisconnected(const QString &ip, int port) ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count())); } +void frmWebServer::error(const QString &ip, int port, const QString &error) +{ + append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg(error)); +} + 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); + append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg(data)); } 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); + append(1, QString("[%1:%2] %3").arg(ip).arg(port).arg(data)); } void frmWebServer::on_btnListen_clicked() @@ -229,7 +253,7 @@ void frmWebServer::on_btnSend_clicked() } } -void frmWebServer::on_btnClose_clicked() +void frmWebServer::on_btnRemove_clicked() { if (ui->ckSelectAll->isChecked()) { server->remove(); diff --git a/nettool/form/frmwebserver.h b/nettool/form/frmwebserver.h index c09b11b..15c19e5 100644 --- a/nettool/form/frmwebserver.h +++ b/nettool/form/frmwebserver.h @@ -16,6 +16,9 @@ public: explicit frmWebServer(QWidget *parent = 0); ~frmWebServer(); +protected: + bool eventFilter(QObject *watched, QEvent *event); + private: Ui::frmWebServer *ui; @@ -31,8 +34,10 @@ private slots: 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 connected(const QString &ip, int port); + void disconnected(const QString &ip, int port); + void error(const QString &ip, int port, const QString &error); + void sendData(const QString &ip, int port, const QString &data); void receiveData(const QString &ip, int port, const QString &data); @@ -41,7 +46,7 @@ private slots: void on_btnSave_clicked(); void on_btnClear_clicked(); void on_btnSend_clicked(); - void on_btnClose_clicked(); + void on_btnRemove_clicked(); }; #endif // FRMWEBSERVER_H diff --git a/nettool/form/frmwebserver.ui b/nettool/form/frmwebserver.ui index 1b3fc76..61b0726 100644 --- a/nettool/form/frmwebserver.ui +++ b/nettool/form/frmwebserver.ui @@ -153,9 +153,9 @@ - + - 断开 + 移除 @@ -262,7 +262,7 @@ btnListen btnSave btnClear - btnClose + btnRemove listWidget ckSelectAll