在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. 摘录-解压版mysql配置(版本5.7)

    1.下载解压2.创建my.ini文件基础配置:(注意编码必须为ANSI)#代码开始[Client]# 设置mysql客户端默认字符集default-character-set=utf8[mysqld] ...

  2. 蓝牙简单配对(Simple Pairing)协议及代码流程简述

    kangear注: 文章转自:http://blog.csdn.net/myxmu/article/details/12217135 原文把图给搞丢了.可是文章太好了,这个时候我就发挥多年的Googl ...

  3. CSS自己主动换行、强制不换行、强制断行、超出显示省略号

    P标签是默认是自己主动换行的,因此设置好宽度之后,可以较好的实现效果,可是近期的项目中发现,使用ajax载入数据之后.p标签内的内容没有换行,导致布局错乱,于是尝试着使用换行样式,尽管攻克了问题.可是 ...

  4. C# WebQQ协议群发机器人(一)

    原创性申明 本文地址 http://blog.csdn.net/zhujunxxxxx/article/details/38931287 转载的话请注明出处. 之前我也写过一篇使用python来实现的 ...

  5. Android多媒体开发(3)————使用Android NKD编译havlenapetr-FFMpeg-7c27aa2

    1. 使用NDK去编译官方的FFmpeg原版的话,还得自己实现JNI层与Java层,工程量比较大.所以移植FFmpeg到Android平台时,可以移植一些已经实现JNI与JAVA层的开源项目,毕竟软件 ...

  6. SCM文章9类:外部中断示例程序

    JP3遇见P0口,JP5遇见P3口,P1接受该发光二极管,什么时候P1所有的都是高时,,全亮度发光二极管.因为外部中断0和1用同样的方法.这里只是外部中断0计划. #include<reg51. ...

  7. Internet protocol optimizer

    A method for optimizing the throughput of TCP/IP applications by aggregating user application data a ...

  8. 华为如何实现基于Git的跨地域协同开发

    跨地域开发的需求其实由来已久,在IT/互联网发展的早期就已存在,只不过限于当时网络环境的因素,无法在线上有效的完成协同工作,所以没法实际开展.而随着近十年网络的快速发展,跨地域协同开发线变得可能而且越 ...

  9. 解析URL查询字符串参数为对象以及老浏览器的getElementsByClassName

    高程3使用拼接字符串形式解析的查询字符串,网上有各种正则方式解析的,记得太多,临时需要写的时候,自己都搞混乱了.只记一种吧,用正则. function getQueryStringArgs() { v ...

  10. Net锁

    Net分布式锁的实现 序言 我晚上有在公司多呆会儿的习惯,所以很多晚上我都是最后一个离开公司的.当然也有一些同事,跟我一样喜欢在公司多搞会儿.这篇文章就要从,去年年末一个多搞会的晚上说起,那是一个夜黑 ...