当前位置:   article > 正文

Qt进程间双向通信(QLocalSocket和QLocalServer)实现_qlocalsocket 双向通信

qlocalsocket 双向通信

Qt环境:Qt4

先上代码

Client:  mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QLocalSocket>
  5. namespace Ui {
  6. class MainWindow;
  7. }
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MainWindow(QWidget *parent = 0);
  13. ~MainWindow();
  14. private slots:
  15. void on_pushButtoLinkServer_clicked();
  16. void OnReadyRead();
  17. void on_pushButtonSendMsg_clicked();
  18. private:
  19. Ui::MainWindow *ui;
  20. QLocalSocket * m_pSocketReceive;
  21. QString m_serverName;
  22. };
  23. #endif // MAINWINDOW_H

mainwindow.cpp :

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow),
  6. m_pSocketReceive(NULL),
  7. m_serverName("ServerName")
  8. {
  9. ui->setupUi(this);
  10. }
  11. MainWindow::~MainWindow()
  12. {
  13. delete ui;
  14. }
  15. void MainWindow::on_pushButtoLinkServer_clicked()
  16. {
  17. if(NULL == m_pSocketReceive)
  18. {
  19. m_pSocketReceive = new QLocalSocket(this);
  20. m_pSocketReceive->connectToServer(m_serverName);
  21. // connect(m_pSocketReceive,SIGNAL(connected()),this,SLOT(OnClientConnectSuccess()));
  22. connect(m_pSocketReceive,SIGNAL(readyRead()),this,SLOT(OnReadyRead()));
  23. }
  24. }
  25. void MainWindow::OnReadyRead()
  26. {
  27. if(m_pSocketReceive)
  28. {
  29. QDataStream tempReadDataStream(m_pSocketReceive);
  30. tempReadDataStream.setVersion(QDataStream::Qt_4_8);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/184730
推荐阅读
相关标签
  

闽ICP备14008679号