From 17c13cbf2f0abce6f4d3f277ab3d0ed571f225b1 Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Sat, 21 Aug 2021 16:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nettool/api/tcpserver.cpp | 1 + nettool/form/frmtcpclient.cpp | 26 +++++++++++++++++++------- nettool/form/frmtcpserver.cpp | 10 +++++++--- nettool/form/frmudpclient.cpp | 12 ++++++++---- nettool/form/frmudpserver.cpp | 10 +++++++--- nettool/form/frmwebclient.cpp | 17 +++++++++++++---- nettool/form/frmwebserver.cpp | 10 +++++++--- 7 files changed, 62 insertions(+), 24 deletions(-) diff --git a/nettool/api/tcpserver.cpp b/nettool/api/tcpserver.cpp index 32f641a..5522900 100644 --- a/nettool/api/tcpserver.cpp +++ b/nettool/api/tcpserver.cpp @@ -44,6 +44,7 @@ bool TcpServer::start() void TcpServer::stop() { remove(); + this->disconnected(); this->close(); } diff --git a/nettool/form/frmtcpclient.cpp b/nettool/form/frmtcpclient.cpp index c0ba775..f064722 100644 --- a/nettool/form/frmtcpclient.cpp +++ b/nettool/form/frmtcpclient.cpp @@ -22,7 +22,7 @@ void frmTcpClient::initForm() 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())); + connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(disconnected())); #else connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected())); #endif @@ -143,10 +143,13 @@ void frmTcpClient::append(int type, const QString &data, bool clear) QString strType; if (type == 0) { strType = "发送"; - ui->txtMain->setTextColor(QColor("darkgreen")); - } else { + ui->txtMain->setTextColor(QColor("#22A3A9")); + } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("red")); + ui->txtMain->setTextColor(QColor("#D64D54")); + } else { + strType = "信息"; + ui->txtMain->setTextColor(QColor("#A279C5")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -159,14 +162,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())); } void frmTcpClient::disconnected() { isOk = false; - socket->abort(); + //socket->abort(); ui->btnConnect->setText("连接"); append(1, "服务器断开"); + //打印下可能的错误信息 + if (socket->error() != QTcpSocket::UnknownSocketError) { + append(2, socket->errorString()); + } } void frmTcpClient::readData() @@ -218,10 +227,13 @@ 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)) - socket->bind(QHostAddress(AppConfig::TcpBindIP), AppConfig::TcpBindPort); + //socket->bind(QHostAddress(AppConfig::TcpBindIP), AppConfig::TcpBindPort); #endif //连接服务器 socket->connectToHost(AppConfig::TcpServerIP, AppConfig::TcpServerPort); diff --git a/nettool/form/frmtcpserver.cpp b/nettool/form/frmtcpserver.cpp index 006a257..e016d1e 100644 --- a/nettool/form/frmtcpserver.cpp +++ b/nettool/form/frmtcpserver.cpp @@ -7,6 +7,7 @@ frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp ui->setupUi(this); this->initForm(); this->initConfig(); + on_btnListen_clicked(); } frmTcpServer::~frmTcpServer() @@ -129,10 +130,13 @@ void frmTcpServer::append(int type, const QString &data, bool clear) QString strType; if (type == 0) { strType = "发送"; - ui->txtMain->setTextColor(QColor("darkgreen")); - } else { + ui->txtMain->setTextColor(QColor("#22A3A9")); + } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("red")); + ui->txtMain->setTextColor(QColor("#D64D54")); + } else { + strType = "信息"; + ui->txtMain->setTextColor(QColor("#A279C5")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); diff --git a/nettool/form/frmudpclient.cpp b/nettool/form/frmudpclient.cpp index 94b2c1e..8a4bd06 100644 --- a/nettool/form/frmudpclient.cpp +++ b/nettool/form/frmudpclient.cpp @@ -128,10 +128,13 @@ void frmUdpClient::append(int type, const QString &data, bool clear) QString strType; if (type == 0) { strType = "发送"; - ui->txtMain->setTextColor(QColor("darkgreen")); - } else { + ui->txtMain->setTextColor(QColor("#22A3A9")); + } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("red")); + ui->txtMain->setTextColor(QColor("#D64D54")); + } else { + strType = "信息"; + ui->txtMain->setTextColor(QColor("#A279C5")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -190,7 +193,8 @@ void frmUdpClient::sendData(const QString &ip, int port, const QString &data) buffer = data.toUtf8(); } - //绑定网卡和端口 + //绑定网卡和端口,没有绑定过才需要绑定 + //采用端口是否一样来判断是为了方便可以直接动态绑定切换端口 if (socket->localPort() != AppConfig::UdpBindPort) { socket->abort(); socket->bind(QHostAddress(AppConfig::UdpBindIP), AppConfig::UdpBindPort); diff --git a/nettool/form/frmudpserver.cpp b/nettool/form/frmudpserver.cpp index f61388a..4b80db4 100644 --- a/nettool/form/frmudpserver.cpp +++ b/nettool/form/frmudpserver.cpp @@ -7,6 +7,7 @@ frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdp ui->setupUi(this); this->initForm(); this->initConfig(); + on_btnListen_clicked(); } frmUdpServer::~frmUdpServer() @@ -124,10 +125,13 @@ void frmUdpServer::append(int type, const QString &data, bool clear) QString strType; if (type == 0) { strType = "发送"; - ui->txtMain->setTextColor(QColor("darkgreen")); - } else { + ui->txtMain->setTextColor(QColor("#22A3A9")); + } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("red")); + ui->txtMain->setTextColor(QColor("#D64D54")); + } else { + strType = "信息"; + ui->txtMain->setTextColor(QColor("#A279C5")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); diff --git a/nettool/form/frmwebclient.cpp b/nettool/form/frmwebclient.cpp index 9fefb11..9bcb374 100644 --- a/nettool/form/frmwebclient.cpp +++ b/nettool/form/frmwebclient.cpp @@ -130,10 +130,13 @@ void frmWebClient::append(int type, const QString &data, bool clear) QString strType; if (type == 0) { strType = "发送"; - ui->txtMain->setTextColor(QColor("darkgreen")); - } else { + ui->txtMain->setTextColor(QColor("#22A3A9")); + } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("red")); + ui->txtMain->setTextColor(QColor("#D64D54")); + } else { + strType = "信息"; + ui->txtMain->setTextColor(QColor("#A279C5")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData); @@ -146,14 +149,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())); } void frmWebClient::disconnected() { isOk = false; - socket->abort(); + //socket->abort(); ui->btnConnect->setText("连接"); append(1, "服务器断开"); + //打印下可能的错误信息 + if (socket->error() != QTcpSocket::UnknownSocketError) { + append(2, socket->errorString()); + } } void frmWebClient::sendData(const QString &data) diff --git a/nettool/form/frmwebserver.cpp b/nettool/form/frmwebserver.cpp index 792404f..84a957a 100644 --- a/nettool/form/frmwebserver.cpp +++ b/nettool/form/frmwebserver.cpp @@ -7,6 +7,7 @@ frmWebServer::frmWebServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmWeb ui->setupUi(this); this->initForm(); this->initConfig(); + on_btnListen_clicked(); } frmWebServer::~frmWebServer() @@ -129,10 +130,13 @@ void frmWebServer::append(int type, const QString &data, bool clear) QString strType; if (type == 0) { strType = "发送"; - ui->txtMain->setTextColor(QColor("darkgreen")); - } else { + ui->txtMain->setTextColor(QColor("#22A3A9")); + } else if (type == 1) { strType = "接收"; - ui->txtMain->setTextColor(QColor("red")); + ui->txtMain->setTextColor(QColor("#D64D54")); + } else { + strType = "信息"; + ui->txtMain->setTextColor(QColor("#A279C5")); } strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);