更新代码

master
feiyangqingyun 2021-08-22 10:57:02 +08:00
parent 81716767f1
commit 7b511d4816
23 changed files with 338 additions and 147 deletions

View File

@ -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();

View File

@ -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();
};

View File

@ -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();
}

View File

@ -13,16 +13,17 @@ private:
QList<TcpClient *> 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();

View File

@ -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)

View File

@ -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);

View File

@ -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);
}

View File

@ -13,16 +13,17 @@ private:
QList<WebClient *> 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();

View File

@ -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))

View File

@ -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);

View File

@ -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();

View File

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

View File

@ -153,9 +153,9 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<widget class="QPushButton" name="btnRemove">
<property name="text">
<string>断开</string>
<string>移除</string>
</property>
</widget>
</item>
@ -262,7 +262,7 @@
<tabstop>btnListen</tabstop>
<tabstop>btnSave</tabstop>
<tabstop>btnClear</tabstop>
<tabstop>btnClose</tabstop>
<tabstop>btnRemove</tabstop>
<tabstop>listWidget</tabstop>
<tabstop>ckSelectAll</tabstop>
</tabstops>

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}
}

View File

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

View File

@ -152,6 +152,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRemove">
<property name="text">
<string>移除</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labCount">
<property name="minimumSize">

View File

@ -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());
}
}
void frmWebClient::error()
{
append(2, socket->errorString());
}
void frmWebClient::sendData(const QString &data)

View File

@ -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);

View File

@ -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();

View File

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

View File

@ -153,9 +153,9 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<widget class="QPushButton" name="btnRemove">
<property name="text">
<string>断开</string>
<string>移除</string>
</property>
</widget>
</item>
@ -262,7 +262,7 @@
<tabstop>btnListen</tabstop>
<tabstop>btnSave</tabstop>
<tabstop>btnClear</tabstop>
<tabstop>btnClose</tabstop>
<tabstop>btnRemove</tabstop>
<tabstop>listWidget</tabstop>
<tabstop>ckSelectAll</tabstop>
</tabstops>