改进代码
This commit is contained in:
@@ -44,6 +44,7 @@ bool TcpServer::start()
|
|||||||
void TcpServer::stop()
|
void TcpServer::stop()
|
||||||
{
|
{
|
||||||
remove();
|
remove();
|
||||||
|
this->disconnected();
|
||||||
this->close();
|
this->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ void frmTcpClient::initForm()
|
|||||||
socket = new QTcpSocket(this);
|
socket = new QTcpSocket(this);
|
||||||
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
|
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
#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
|
#else
|
||||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
|
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
|
||||||
#endif
|
#endif
|
||||||
@@ -143,10 +143,13 @@ void frmTcpClient::append(int type, const QString &data, bool clear)
|
|||||||
QString strType;
|
QString strType;
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
strType = "发送";
|
strType = "发送";
|
||||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||||
} else {
|
} else if (type == 1) {
|
||||||
strType = "接收";
|
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);
|
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||||
@@ -159,14 +162,20 @@ void frmTcpClient::connected()
|
|||||||
isOk = true;
|
isOk = true;
|
||||||
ui->btnConnect->setText("断开");
|
ui->btnConnect->setText("断开");
|
||||||
append(0, "服务器连接");
|
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()
|
void frmTcpClient::disconnected()
|
||||||
{
|
{
|
||||||
isOk = false;
|
isOk = false;
|
||||||
socket->abort();
|
//socket->abort();
|
||||||
ui->btnConnect->setText("连接");
|
ui->btnConnect->setText("连接");
|
||||||
append(1, "服务器断开");
|
append(1, "服务器断开");
|
||||||
|
//打印下可能的错误信息
|
||||||
|
if (socket->error() != QTcpSocket::UnknownSocketError) {
|
||||||
|
append(2, socket->errorString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void frmTcpClient::readData()
|
void frmTcpClient::readData()
|
||||||
@@ -218,10 +227,13 @@ void frmTcpClient::on_btnConnect_clicked()
|
|||||||
{
|
{
|
||||||
if (ui->btnConnect->text() == "连接") {
|
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))
|
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||||
socket->bind(QHostAddress(AppConfig::TcpBindIP), AppConfig::TcpBindPort);
|
//socket->bind(QHostAddress(AppConfig::TcpBindIP), AppConfig::TcpBindPort);
|
||||||
#endif
|
#endif
|
||||||
//连接服务器
|
//连接服务器
|
||||||
socket->connectToHost(AppConfig::TcpServerIP, AppConfig::TcpServerPort);
|
socket->connectToHost(AppConfig::TcpServerIP, AppConfig::TcpServerPort);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcp
|
|||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
this->initForm();
|
this->initForm();
|
||||||
this->initConfig();
|
this->initConfig();
|
||||||
|
on_btnListen_clicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
frmTcpServer::~frmTcpServer()
|
frmTcpServer::~frmTcpServer()
|
||||||
@@ -129,10 +130,13 @@ void frmTcpServer::append(int type, const QString &data, bool clear)
|
|||||||
QString strType;
|
QString strType;
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
strType = "发送";
|
strType = "发送";
|
||||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||||
} else {
|
} else if (type == 1) {
|
||||||
strType = "接收";
|
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);
|
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||||
|
|||||||
@@ -128,10 +128,13 @@ void frmUdpClient::append(int type, const QString &data, bool clear)
|
|||||||
QString strType;
|
QString strType;
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
strType = "发送";
|
strType = "发送";
|
||||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||||
} else {
|
} else if (type == 1) {
|
||||||
strType = "接收";
|
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);
|
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();
|
buffer = data.toUtf8();
|
||||||
}
|
}
|
||||||
|
|
||||||
//绑定网卡和端口
|
//绑定网卡和端口,没有绑定过才需要绑定
|
||||||
|
//采用端口是否一样来判断是为了方便可以直接动态绑定切换端口
|
||||||
if (socket->localPort() != AppConfig::UdpBindPort) {
|
if (socket->localPort() != AppConfig::UdpBindPort) {
|
||||||
socket->abort();
|
socket->abort();
|
||||||
socket->bind(QHostAddress(AppConfig::UdpBindIP), AppConfig::UdpBindPort);
|
socket->bind(QHostAddress(AppConfig::UdpBindIP), AppConfig::UdpBindPort);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ frmUdpServer::frmUdpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmUdp
|
|||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
this->initForm();
|
this->initForm();
|
||||||
this->initConfig();
|
this->initConfig();
|
||||||
|
on_btnListen_clicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
frmUdpServer::~frmUdpServer()
|
frmUdpServer::~frmUdpServer()
|
||||||
@@ -124,10 +125,13 @@ void frmUdpServer::append(int type, const QString &data, bool clear)
|
|||||||
QString strType;
|
QString strType;
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
strType = "发送";
|
strType = "发送";
|
||||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||||
} else {
|
} else if (type == 1) {
|
||||||
strType = "接收";
|
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);
|
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||||
|
|||||||
@@ -130,10 +130,13 @@ void frmWebClient::append(int type, const QString &data, bool clear)
|
|||||||
QString strType;
|
QString strType;
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
strType = "发送";
|
strType = "发送";
|
||||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||||
} else {
|
} else if (type == 1) {
|
||||||
strType = "接收";
|
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);
|
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||||
@@ -146,14 +149,20 @@ void frmWebClient::connected()
|
|||||||
isOk = true;
|
isOk = true;
|
||||||
ui->btnConnect->setText("断开");
|
ui->btnConnect->setText("断开");
|
||||||
append(0, "服务器连接");
|
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()
|
void frmWebClient::disconnected()
|
||||||
{
|
{
|
||||||
isOk = false;
|
isOk = false;
|
||||||
socket->abort();
|
//socket->abort();
|
||||||
ui->btnConnect->setText("连接");
|
ui->btnConnect->setText("连接");
|
||||||
append(1, "服务器断开");
|
append(1, "服务器断开");
|
||||||
|
//打印下可能的错误信息
|
||||||
|
if (socket->error() != QTcpSocket::UnknownSocketError) {
|
||||||
|
append(2, socket->errorString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void frmWebClient::sendData(const QString &data)
|
void frmWebClient::sendData(const QString &data)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ frmWebServer::frmWebServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmWeb
|
|||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
this->initForm();
|
this->initForm();
|
||||||
this->initConfig();
|
this->initConfig();
|
||||||
|
on_btnListen_clicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
frmWebServer::~frmWebServer()
|
frmWebServer::~frmWebServer()
|
||||||
@@ -129,10 +130,13 @@ void frmWebServer::append(int type, const QString &data, bool clear)
|
|||||||
QString strType;
|
QString strType;
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
strType = "发送";
|
strType = "发送";
|
||||||
ui->txtMain->setTextColor(QColor("darkgreen"));
|
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||||
} else {
|
} else if (type == 1) {
|
||||||
strType = "接收";
|
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);
|
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||||
|
|||||||
Reference in New Issue
Block a user