更新代码

This commit is contained in:
feiyangqingyun
2021-08-22 10:57:24 +08:00
parent 17c13cbf2f
commit 885531e848
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,15 +13,16 @@ private:
QList<WebClient *> 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:
//启动服务