QLocalServer与QLocalSocket进程通讯

在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

时间: 2025-01-04 22:49:50

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

【转】android中跨进程通讯的4种方式

转自:http://www.androidsdn.com/article/show/137 由于android系统中应用程序之间不能共享内存.因此,在不同应用程序之间交互数据(跨进程通讯)就稍微麻烦一些.在android SDK中提供了4种用于跨进程通讯的方式.这4种方式正好对应于android系统中4种应用程序组件:Activity.Content Provider.Broadcast和Service. 其中Activity可以跨进程调用其他应用程序的Activity: Content Pro

Android间的进程通讯(传递复杂对象)

Android间的进程通讯(传递复杂对象) 完成对复杂对象的序列化 在Android中传递复杂数据类型的时候要通过将序列化,在Android中提供了一个接口Parcelable来实现对对象的序列化. 下面对需要传输的对象进行序列化操作,首先看自定义的类Person. package com.example.service_parcelable_conmmute.bean; import android.graphics.Bitmap; /** * 用来传输的对象结构 * @author Xiny

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

不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递message对象,在message中放入我们需要传递的数据你就可以实现跨进程通讯和传递数据.废话不多说,直接上代码. 首先是服务端: public class Ser extends Service{ @Override public IBinder onBind(Intent intent) {

大数据技术之_16_Scala学习_11_客户信息管理系统+并发编程模型 Akka+Akka 网络编程-小黄鸡客服案例+Akka 网络编程-Spark Master Worker 进程通讯项目

第十五章 客户信息管理系统15.1 项目的开发流程15.2 项目的需求分析15.3 项目的界面15.4 项目的设计-程序框架图15.5 项目的功能实现15.5.1 完成 Customer 类15.5.2 完成显示主菜单和退出软件功能15.5.3 完成显示客户列表的功能15.5.4 完成添加客户的功能15.5.5 完成删除客户的功能15.5.6 完善退出确认功能15.5.7 完善删除确认功能15.5.8 完成修改客户的功能第十六章 并发编程模型 Akka16.1 Akka 的介绍16.2 Acto

android ipc通信机制之之三,进程通讯方式。

IPC通讯方式的优缺点: IPC通讯方式的对比 名称 优点 缺点 适用场景 Bundle 简单易用 只能传输Bundle支持的数据类型 四大组件的进程通信 文件共享 简单易用 不适合高并发场景,并无法做到进程间即时通讯. 无并发访问情形,交换简单的数据是实时性不高的场景. AIDL 功能强大,支持一对多并发通信,支持实时通信. 使用稍微复杂,需要处理好线程同步. 一对多通信且有RPC需求 Messenger 功能一般,支持一对多串行通信,支持实时通信. 不能很好处理高并发情形,不支持RPC,数据

Android四大组件应用系列——使用ContentProvider实现跨进程通讯

一.问题描述 如何在Android中实现不同应用之间的通讯(既跨进程进行调用)?Android提供了多种实现方式,使我们可以实现跨进程访问Activity.通过ContentProvider跨进程访问其他应用的数据.通过Broadcast可以向android系统中所有应用程序发送广播.使用AIDL实现跨进程的Service.下面我们就使用ContentProvider实现跨进程访问数据,并可对数据进行增.删.改.查 二.应用实现 使用ContentProvider实现数据共享,主要是共享应用的S

进程和线程的定义及区别、线程同步、进程通讯方式总结

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一. 进程的概念 进程是在多道程序系统出现以后,为了描述系统内部各作业的活动规律而引进的概念. 由 于多道程序系统所带来的复杂环境,程序本身有了并行性[为了充分利用资源,在主存中同时存放多道作业运行,所以各作业之间是并行的].制约性[各程序由于 同时存在于主存中,因此他们之间会存在着相互依赖.相互制约的关系.一个是通过中间媒介--资源发生的间接制约关系,一个是各并行程序间需要相互协同而引 起

内存映射实现进程通讯

unit FileMap; interface uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,  StdCtrls, Dialogs; type  //定义TFileMap类  TFileMap = class(TComponent)  private    FMapHandle: THandle; //内存映射文件句柄    FMutexHandle: THandle; //互斥句柄    FMapN

Linux_C bc/利用2根管道让2进程通讯

1 /* tingbc.c 2 * use two pipe to execute the bc. 3 * one pipe: todc[2] , another: fromdc[2] 4 * child thread to do dc, parent do UI 5 */ 6 7 #include <stdio.h> 8 #include <unistd.h> 9 #include <string.h> 10 #define oops(x, n) {perror(x)