在Qt中,提供了多种IPC方法,作者所用的是QLocalServer和QLocalSocket。看起来好像和Socket搭上点边,实则底层是windows的name pipe。这应该是支持双工通信的。
 
一 QLocalServer
 
#ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H
#include 
#include 
#include
class QPushButton;
class QTextEdit;
class CVxMainWindow : public QWidget
{
 Q_OBJECT
public:
 CVxMainWindow(QWidget *parent=NULL);
 ~CVxMainWindow();
protected:
 void resizeEvent(QResizeEvent *);
private slots:
 void Btn_ListenClickedSlot();
 void Btn_StopListenClickedSlot();
 void newConnectionSlot();
 void dataReceived();
private:
 QLocalServer *m_pServer;
 QLocalSocket *m_pSocket;
 QPushButton *m_pBtn_Listen;
 QPushButton *m_pBtn_StopListen;
 QTextEdit   *m_pEdt_Info;
};
#endif // VXMAINWINDOW_H
#include "VxMainWindow.h"
#include
CVxMainWindow::CVxMainWindow(QWidget *parent)
 : QWidget(parent)
{
 m_pBtn_Listen     = new QPushButton(QObject::tr("开始监听"), this);
 m_pBtn_StopListen = new QPushButton(QObject::tr("停止监听"), this);
 m_pEdt_Info       = new QTextEdit(this);
 m_pServer         = new QLocalServer(this);
 connect(m_pBtn_Listen,     SIGNAL(clicked()), this, SLOT(Btn_ListenClickedSlot()));
 connect(m_pBtn_StopListen, SIGNAL(clicked()), this, SLOT(Btn_StopListenClickedSlot()));
 connect(m_pServer,         SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}
CVxMainWindow::~CVxMainWindow()
{
}
void CVxMainWindow::resizeEvent(QResizeEvent *)
{
 m_pBtn_Listen->setGeometry(10, 5, 80, 20);
 m_pBtn_StopListen->setGeometry(100, 5, 80, 20);
 m_pEdt_Info->setGeometry(0, 30, width(), height() - 30);
}
void CVxMainWindow::Btn_ListenClickedSlot()
{
 if (!m_pServer->isListening())
 {
  if (m_pServer->listen(QObject::tr("AAA"))) 
  {
   m_pEdt_Info->append(QObject::tr("打开监听端口成功!"));
  }
  else
  {
   m_pEdt_Info->append(QObject::tr("打开监听端口失败!"));
  }
 }
 else
 {
  m_pEdt_Info->append(QObject::tr("正在监听中...!"));
 }
}
void CVxMainWindow::Btn_StopListenClickedSlot()
{
 if (m_pServer->isListening())
 {
  m_pServer->close();
  m_pEdt_Info->append(QObject::tr("停止监听!"));
 }
}
void CVxMainWindow::newConnectionSlot()
{
 m_pEdt_Info->append(QObject::tr("有新客户端连接到服务器"));
 m_pSocket = m_pServer->nextPendingConnection();
 connect(m_pSocket, SIGNAL(disconnected()), m_pSocket, SLOT(deleteLater()));
 connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
 int length = 0;
 QString vMsgStr = QObject::tr("Welcome");
 if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
 {
 }
}
void CVxMainWindow::dataReceived()
{
 while(m_pSocket->bytesAvailable())
 {       
  QString vTemp;
  vTemp = m_pSocket->readLine();          
  m_pEdt_Info->append(vTemp);
  int length = 0;
  QString vMsgStr = QObject::tr("回复:") + vTemp;
  if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
  {
  }
 }
}
 
二 QLocalSocket
 
#ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H
#include 
#include
class QPushButton;
class QTextEdit;
class QLineEdit;
class CVxMainWindow : public QWidget
{
 Q_OBJECT
public:
 CVxMainWindow(QWidget *parent=NULL);
 ~CVxMainWindow();
protected:
 void resizeEvent(QResizeEvent *);
 private slots:
  void Btn_ConnectClickedSlot();
  void Btn_DisConnectClickedSlot();
  void Btn_SendClickedSlot();
  void connectedSlot();
  void disconnectedSlot();
  void dataReceived();
  void displayError(QAbstractSocket::SocketError);
private:
 QLocalSocket *m_pSocket;
 QPushButton *m_pBtn_Connect;
 QPushButton *m_pBtn_DisConnect;
 QTextEdit   *m_pEdt_Info;
 QLineEdit   *m_pEdt_Send;
 QPushButton *m_pBtn_Send;
};
#endif // VXMAINWINDOW_H
#include "VxMainWindow.h"
#include
CVxMainWindow::CVxMainWindow(QWidget *parent)
 : QWidget(parent)
{
 m_pBtn_Connect    = new QPushButton(QObject::tr("连接服务器"), this);
 m_pBtn_DisConnect = new QPushButton(QObject::tr("断开连接"), this);
 m_pEdt_Send       = new QLineEdit(this);
 m_pBtn_Send       = new QPushButton(QObject::tr("发送"), this);
 m_pEdt_Info = new QTextEdit(this);
 m_pSocket = new QLocalSocket(this);
 connect(m_pBtn_Connect,    SIGNAL(clicked()), this, SLOT(Btn_ConnectClickedSlot()));
 connect(m_pBtn_DisConnect, SIGNAL(clicked()), this, SLOT(Btn_DisConnectClickedSlot()));
 connect(m_pBtn_Send,       SIGNAL(clicked()), this, SLOT(Btn_SendClickedSlot()));
 connect(m_pSocket, SIGNAL(connected()), this, SLOT(connectedSlot()));
 connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
 connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
 connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
}
CVxMainWindow::~CVxMainWindow()
{
}
void CVxMainWindow::resizeEvent(QResizeEvent *)
{
 m_pBtn_Connect->setGeometry(10, 5, 80, 20);
 m_pBtn_DisConnect->setGeometry(100, 5, 80, 20);
 m_pEdt_Send->setGeometry(10, 30, 150, 20);
 m_pBtn_Send->setGeometry(170, 30, 80, 20);
 m_pEdt_Info->setGeometry(0, 60, width(), height() - 60);
}
void CVxMainWindow::Btn_ConnectClickedSlot()
{
 m_pSocket->connectToServer(QObject::tr("AAA"));
}
void CVxMainWindow::Btn_DisConnectClickedSlot()
{
 m_pSocket->disconnectFromServer();
}
void CVxMainWindow::Btn_SendClickedSlot()
{
 int length = 0;
 QString vMsgStr = m_pEdt_Send->text();
 if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
 {
  m_pEdt_Info->append(QObject::tr("发送信息失败:") + vMsgStr);
 }
}
void CVxMainWindow::connectedSlot()
{
 m_pEdt_Info->append(QObject::tr("成功连接到服务器!"));
}
void CVxMainWindow::disconnectedSlot()
{
 m_pEdt_Info->append(QObject::tr("断开与服务器的连接!"));
}
void CVxMainWindow::dataReceived()
{
 while(m_pSocket->bytesAvailable())
 {       
  QString vTemp;
  vTemp = m_pSocket->readLine();          
  m_pEdt_Info->append(vTemp);
 }
}
void CVxMainWindow::displayError(QAbstractSocket::SocketError)
{
}
http://blog.chinaunix.net/uid-20718335-id-1993073.html

QLocalServer与QLocalSocket进程通讯的更多相关文章

  1. QLocalServer和QLocalSocket单进程和进程通信

    QLocalServer 继承自QObject. QLocalServer提供了一个基于本地套接字(socket)的服务端(server).QLocalServer可以接受来自本地socket的连接. ...

  2. android 史上最简单易懂的跨进程通讯(Messenger)!

    不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递m ...

  3. linux 下进程通讯详解

    linux 下进程通讯方法主要有以下六种: 1.管道 2.信号 3.共享内存 4.消息队列 5.信号量 6.socket

  4. android中跨进程通讯的4种方式

    转自:http://blog.csdn.net/lyf_007217/article/details/8542359 帖子写的很好.看来一遍,试了一遍,感觉太有意义.必须转过来! android中跨进 ...

  5. Chris Richardson微服务翻译:构建微服务之微服务架构的进程通讯

    Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯(本文) 微服务架构中的服务发现 微服务之事件驱动的数据管理 微服 ...

  6. Android查缺补漏(IPC篇)-- 款进程通讯之AIDL详解

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  7. Android为TV端助力 史上最简单易懂的跨进程通讯(Messenger)!

    不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递m ...

  8. .NET 4.0中使用内存映射文件实现进程通讯

    操作系统很早就开始使用内存映射文件(Memory Mapped File)来作为进程间的共享存储区,这是一种非常高效的进程通讯手段.Win32 API中也包含有创建内存映射文件的函数,然而,这些函数都 ...

  9. android 跨进程通讯 AIDL

    跨进程如何通讯?两个进程无法直接通讯,通过Android系统底层间接通讯.基于service的aidl实现跨进程通讯. 什么叫AIDL? Android interface definition la ...

随机推荐

  1. 切换-5.7-GTID复制切换成传统复制

    mysql5.7 gtid和传统复制在线切换,5.7.6 之后 不用重启可以直接在线切换   基本环境   Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL ...

  2. Ehcache配置

    http://blog.csdn.net/lwx2615/article/details/5624388 http://www.cnblogs.com/hoojo/archive/2012/07/12 ...

  3. 新技能 get —— 使用 python 生成词云

    什么是词云(word cloud)呢?词云又叫文字云,是对文本数据中出现频率较高的"关键词"在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据 ...

  4. springboot 使用传统方式部署

    spring boot默认创建出来的应用程序是内嵌web容器的,直接运行jar文件就可以的,但通常我们也需要将程序部署到tomcat中,这需要做如下改进: 1.pom.xml修改 打包方式需要修改成w ...

  5. 《⑨也懂系列:MinGW-w64安装教程》著名C/C++编译器GCC的Windows版本(MinGW-w64在安装的时候可以选择版本,有图,一步一步)

    发布日期 2016年10月31日 分类 教程 标签 编程.软件 前言<⑨也懂系列:MinGW-w64安装教程>这篇文章由 rsreland (http://rsreland.net)于 2 ...

  6. Hibernate——(6)延迟加载机制

    一.延迟加载机制的基本原理 当访问实体对象时,并不是立即到数据库中查找.而是在真正要使用实体对象的时候,才去数据库查询数据. 具备这样功能的方法 session.load(...) query.ite ...

  7. 设计模式在JDK中的应用

    在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式. 创建型模式: (1) 抽象工厂模式(Abstract Factory) • java.util.Calenda ...

  8. 新世界PT850/PT853检查用友机对接T1交易宝

    新世界PT850/PT853检查用友机对接T1交易宝 这是一个老话题.简单.条形码号码模式是没有必要说,我要说的是,,启用条码扫描.显示有关信息. 并能够产生:条码 , 数量,价格格 这样做的目的.将 ...

  9. 组态档(configuration file)与建构档

    组态档,或者叫 configuration file,配置文件.组态档是用一种建构软件专用的特殊编程语言写的 CMake 脚本. 使用组态档能改变程序的设置,而不用重新编译程序. CMake 的组态档 ...

  10. Python Tricks(二十一)—— 排列组合的计算

    使用循环: 阶乘的实现: def fac(n): r = 1. for i in range(1, n+1): r *= i return r 排列:Anm=m!n!=(m−n+1)⋯m def pe ...