当前位置:   article > 正文

Qt QWebSocketServer网络编程 服务端和客户端

qwebsocket

        QWebSocketServer以QTcpServer为模型,行为相同,我们对QTcpServer使用方法有了解的,会很快对该类上手。

enum QWebSocketServer::SslMode:指示服务器运行模式。

SecureMode:服务器以安全模式运行(wss)。

NonSecureMode:服务器以非安全模式运行(ws)。

简介TCP三次握手:

服务端: 

头文件

  1. #ifndef QWEBSOCKETWIDGET_H
  2. #define QWEBSOCKETWIDGET_H
  3. #include <QWidget>
  4. #include <QWebSocket>
  5. #include <QDateTime>
  6. QT_BEGIN_NAMESPACE
  7. namespace Ui { class QWebSocketWidget; }
  8. QT_END_NAMESPACE
  9. class QWebSocketWidget : public QWidget
  10. {
  11. Q_OBJECT
  12. public:
  13. QWebSocketWidget(QWidget *parent = nullptr);
  14. ~QWebSocketWidget();
  15. private slots:
  16. void on_ConnectBtn_clicked();
  17. void on_disConnectBtn_clicked();
  18. void on_sendDataBtn_clicked();
  19. void on_clearlogDataBtn_clicked();
  20. void onDisconnected();
  21. void onConnected();
  22. void onTextMessageRecived(QString str);
  23. private:
  24. QWebSocket Websocket;
  25. private:
  26. Ui::QWebSocketWidget *ui;
  27. };
  28. #endif // QWEBSOCKETWIDGET_H

源文件

  1. #include "qwebsocketwidget.h"
  2. #include "ui_qwebsocketwidget.h"
  3. QWebSocketWidget::QWebSocketWidget(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::QWebSocketWidget)
  6. {
  7. ui->setupUi(this);
  8. //连接信号和槽函数
  9. setWindowTitle("客户端");
  10. connect(&Websocket,&QWebSocket::disconnected,this,&QWebSocketWidget::onDisconnected);//客户端断开连接
  11. connect(&Websocket,&QWebSocket::connected,this,&QWebSocketWidget::onConnected);//客户端连接服务端
  12. connect(&Websocket,&QWebSocket::textMessageReceived,this,&QWebSocketWidget::onTextMessageRecived);//客户端收到服务端消息
  13. }
  14. QWebSocketWidget::~QWebSocketWidget()
  15. {
  16. delete ui;
  17. }
  18. //连接服务端
  19. void QWebSocketWidget::on_ConnectBtn_clicked()
  20. {
  21. ui->LogtextEdit->setText(QDateTime::currentDateTime().toString()+":开始连接");
  22. QString urlStr = QString("ws://%1").arg(ui->portlineEdit->text());//地址和端口
  23. Websocket.open(QUrl(urlStr));//套接字获得地址和端口的属性
  24. }
  25. //断开服务端
  26. void QWebSocketWidget::on_disConnectBtn_clicked()
  27. {
  28. Websocket.close();
  29. }
  30. //发送数据到服务端
  31. void QWebSocketWidget::on_sendDataBtn_clicked()
  32. {
  33. QString str = QDateTime::currentDateTime().toString()+":给服务端发送消息:"+ui->SendDatatextEdit->toPlainText();
  34. ui->LogtextEdit->setText(str);
  35. Websocket.sendTextMessage(ui->SendDatatextEdit->toPlainText());//给服务端发消息
  36. }
  37. //清空日志信息
  38. void QWebSocketWidget::on_clearlogDataBtn_clicked()
  39. {
  40. ui->LogtextEdit->clear();
  41. }
  42. void QWebSocketWidget::onDisconnected()
  43. {
  44. QString str = QDateTime::currentDateTime().toString()+"客户端断开连接!";
  45. ui->LogtextEdit->setText(str);
  46. }
  47. void QWebSocketWidget::onConnected()
  48. {
  49. QString str = QDateTime::currentDateTime().toString()+":客户端连接服务端!";
  50. ui->LogtextEdit->setText(str);
  51. }
  52. void QWebSocketWidget::onTextMessageRecived(QString str)
  53. {
  54. QString str1 = QDateTime::currentDateTime().toString()+":服务端发来消息:"+str;
  55. ui->LogtextEdit->setText(str1);
  56. }

客户端:

头文件

  1. #ifndef WEBSOCKETSERVERWIDGET_H
  2. #define WEBSOCKETSERVERWIDGET_H
  3. #include <QWidget>
  4. #include <QWebSocketServer>
  5. #include <QWebSocket>
  6. #include <QDateTime>
  7. QT_BEGIN_NAMESPACE
  8. namespace Ui { class webSocketServerWidget; }
  9. QT_END_NAMESPACE
  10. class webSocketServerWidget : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. webSocketServerWidget(QWidget *parent = nullptr);
  15. ~webSocketServerWidget();
  16. private slots:
  17. void on_StartListen_clicked();//开始监听
  18. void on_endListen_clicked();//结束监听
  19. void OnNewConnection();//新连接接入服务端
  20. void onTextMessageReived(QString str);//接受客户端发送的消息
  21. void onDisconnected();//客户端断开连接给服务端发消息
  22. void on_sendData_clicked();
  23. void on_clearLog_clicked();
  24. private:
  25. QWebSocketServer* socketServer;//服务端
  26. QWebSocket* websocket;//客户端
  27. private:
  28. Ui::webSocketServerWidget *ui;
  29. };
  30. #endif // WEBSOCKETSERVERWIDGET_H

源文件

  1. #include "websocketserverwidget.h"
  2. #include "ui_websocketserverwidget.h"
  3. webSocketServerWidget::webSocketServerWidget(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::webSocketServerWidget)
  6. {
  7. ui->setupUi(this);
  8. setWindowTitle("服务端");
  9. socketServer = new QWebSocketServer("Server",QWebSocketServer::NonSecureMode);//采用非安全模式
  10. //客户端有新连接接入时,服务端会触发这个信号
  11. connect(socketServer,&QWebSocketServer::newConnection,this,&webSocketServerWidget::OnNewConnection);
  12. }
  13. webSocketServerWidget::~webSocketServerWidget()
  14. {
  15. delete ui;
  16. }
  17. void webSocketServerWidget::on_StartListen_clicked()
  18. {
  19. if(ui->StartListen->text().isEmpty())
  20. return;
  21. int port = ui->lineEditPort->text().toInt();
  22. socketServer->listen(QHostAddress::Any,port);//服务端套接字监听(ip地址,端口)
  23. ui->logTextLine->setText(QDateTime::currentDateTime().toString()+":开始监听");
  24. }
  25. void webSocketServerWidget::OnNewConnection()
  26. {
  27. websocket = socketServer->nextPendingConnection();
  28. connect(websocket,&QWebSocket::textMessageReceived,this,&webSocketServerWidget::onTextMessageReived);
  29. connect(websocket,&QWebSocket::disconnected,this,&webSocketServerWidget::onDisconnected);
  30. }
  31. void webSocketServerWidget::onTextMessageReived(QString str)
  32. {
  33. ui->logTextLine->setText(QDateTime::currentDateTime().toString()+":有新的连接进入,"+str);
  34. }
  35. void webSocketServerWidget::onDisconnected()
  36. {
  37. ui->logTextLine->setText(QDateTime::currentDateTime().toString()+"客户端断开连接");
  38. }
  39. void webSocketServerWidget::on_endListen_clicked()
  40. {
  41. ui->log->setText(QDateTime::currentDateTime().toString()+":停止监听");
  42. if(socketServer->isListening())
  43. socketServer->close();
  44. }
  45. void webSocketServerWidget::on_sendData_clicked()
  46. {
  47. QString msg = ui->textEditSendData->toPlainText();
  48. websocket->sendTextMessage(msg);
  49. ui->logTextLine->setText(QDateTime::currentDateTime().toString()+":服务端发送消息:"+ui->textEditSendData->toPlainText());
  50. }
  51. void webSocketServerWidget::on_clearLog_clicked()
  52. {
  53. ui->logTextLine->clear();
  54. }

资源链接

Qt网络编程QWebSocketServer和QWebSocket客户端和网络端demo-C++文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/894075
推荐阅读
相关标签
  

闽ICP备14008679号