QLocalServer与QLocalSocket进程通讯
#define VXMAINWINDOW_H
#include
#include
class QTextEdit;
{
Q_OBJECT
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_StopListen;
QTextEdit *m_pEdt_Info;
};
: 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_StopListen, SIGNAL(clicked()), this, SLOT(Btn_StopListenClickedSlot()));
connect(m_pServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}
{
{
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);
}
{
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("正在监听中...!"));
}
}
{
if (m_pServer->isListening())
{
m_pServer->close();
m_pEdt_Info->append(QObject::tr("停止监听!"));
}
}
{
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()));
QString vMsgStr = QObject::tr("Welcome");
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
QString vMsgStr = QObject::tr("回复:") + vTemp;
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
}
#define VXMAINWINDOW_H
#include
class QTextEdit;
class QLineEdit;
{
Q_OBJECT
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_DisConnect;
QTextEdit *m_pEdt_Info;
QLineEdit *m_pEdt_Send;
QPushButton *m_pBtn_Send;
};
: 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_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)));
}
{
{
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);
}
{
m_pSocket->connectToServer(QObject::tr("AAA"));
}
{
m_pSocket->disconnectFromServer();
}
{
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);
}
}
{
m_pEdt_Info->append(QObject::tr("成功连接到服务器!"));
}
{
m_pEdt_Info->append(QObject::tr("断开与服务器的连接!"));
}
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
}
}
{
QLocalServer与QLocalSocket进程通讯的更多相关文章
- QLocalServer和QLocalSocket单进程和进程通信
QLocalServer 继承自QObject. QLocalServer提供了一个基于本地套接字(socket)的服务端(server).QLocalServer可以接受来自本地socket的连接. ...
- android 史上最简单易懂的跨进程通讯(Messenger)!
不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递m ...
- linux 下进程通讯详解
linux 下进程通讯方法主要有以下六种: 1.管道 2.信号 3.共享内存 4.消息队列 5.信号量 6.socket
- android中跨进程通讯的4种方式
转自:http://blog.csdn.net/lyf_007217/article/details/8542359 帖子写的很好.看来一遍,试了一遍,感觉太有意义.必须转过来! android中跨进 ...
- Chris Richardson微服务翻译:构建微服务之微服务架构的进程通讯
Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯(本文) 微服务架构中的服务发现 微服务之事件驱动的数据管理 微服 ...
- Android查缺补漏(IPC篇)-- 款进程通讯之AIDL详解
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android为TV端助力 史上最简单易懂的跨进程通讯(Messenger)!
不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递m ...
- .NET 4.0中使用内存映射文件实现进程通讯
操作系统很早就开始使用内存映射文件(Memory Mapped File)来作为进程间的共享存储区,这是一种非常高效的进程通讯手段.Win32 API中也包含有创建内存映射文件的函数,然而,这些函数都 ...
- android 跨进程通讯 AIDL
跨进程如何通讯?两个进程无法直接通讯,通过Android系统底层间接通讯.基于service的aidl实现跨进程通讯. 什么叫AIDL? Android interface definition la ...
随机推荐
- Android JobService的使用及源码分析
Google在Android 5.0中引入JobScheduler来执行一些需要满足特定条件但不紧急的后台任务,APP利用JobScheduler来执行这些特殊的后台任务时来减少电量的消耗.本文首先介 ...
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...
- 数组filter方法对数组元素进行过滤
Array.prototype.filter对数组中元素进行过滤 /** * @method reduce * @param {number} item 当前迭代的数组元素 * @param {num ...
- .gitignore 设置忽略上传的文件
首先在一个项目中新建如下所示文件用来测试 image.png 一.生成.gitignore文件 1.进入项目根目录,打开终端: 2.输入 vi .gitignore 创建并打开隐藏文件.gitigno ...
- Android系统编译环境初始化时Product产品的import-nodes过程
从运行make -f config,mk文件開始,config,mk作为当前的makefile文件.将会被make解析,一般make解析Makefile文件流程首先是载入当中include的各种其它m ...
- [Angular] Working with FormArray
'FormArray' can work with array of 'FormGroup' or 'FormControl'. form = new FormGroup({ stock: new F ...
- 使用Ant包装时,包javax.servlet.http有没有搞错
明确,出现此错误的原因是缺乏相应的jar包.详细原因因为servlet和JSP不是Java平台JavaSE(标准版)的一部分.而是Java EE(企业版)的一部分,因此,必须告知编译器servlet的 ...
- 阿里云centos7.2自己安装mysql5.7远程不能访问解决方案
版权声明:转载也行 https://blog.csdn.net/u010955892/article/details/72774920 最近,无意中看到阿里云服务器降价,所以一时手痒,买了一年的服务器 ...
- 关于Vuex可直接修改state问题
下面的代码证明不通过mutation,而直接修改state修改确实生效了.这样子多人协作岂不是很容易出问题.对于这个问题,在创建 store 的时候传入 strict: true, 开启严格模式,那么 ...
- [MFC]SDI在图片背景上实现文本跟随鼠标移动
SDI是单文档接口应用程序的简称.本文要实现的是在视图区域显示一张图片,然后在图片表层显示文字,并且文字跟随鼠标移动.思考一下,可以判断这个问题一共分为以下几个部分:1.显示图片:2.找到鼠标的位置: ...