栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

android命名管道创建使用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android命名管道创建使用

#创建命名管道

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define _SEND_ "/data/user/0/person.wangchen11.xqceditor/files/.ashmem0"
#define _RECV_ "/data/user/0/person.wangchen11.xqceditor/files/.ashmem1"

#define _SIZE_ 100

int createPipe(std::string path){
	
	int ret;
	ret = mkfifo(path.c_str(),S_IFIFO|0666);
	if(ret==0)
	{
		std::cout<<"创建成功n";
		return 1;
		
	}
	
	std::cout<<"创建失败n再次创建n";
	
	char ml[64];
	sprintf(ml,"rm -rf %s",path.c_str());
	system(ml);
	
	ret = mkfifo(path.c_str(),S_IFIFO|0666);
	if(ret==0)
	{
		std::cout<<"创建成功n";
		return 1;
		
	}
	std::cout<<"创建失败。。。꒦ິ^꒦ິ。。手动创建";
	return 0;
	
}

int main(){
	
	createPipe(_SEND_);
	createPipe(_RECV_);
	
}

#写入管道

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define _SEND_ "/data/user/0/person.wangchen11.xqceditor/files/.ashmem0"
#define _RECV_ "/data/user/0/person.wangchen11.xqceditor/files/.ashmem1"

#define _SIZE_ 100

void clearBuff(std::stringstream&sstream){
	sstream.str("");//清空buffer
	sstream.clear();//初始化流
}

FILE*_send;
FILE*_recv;

void send(std::string&str)
{
	if(NULL!=(_send = fopen(_SEND_,"w")))
	{
		fwrite(str.c_str(),1,str.length(),_send);
		fclose(_send);
	}
}

char buffer[_SIZE_];

void recv(std::string&str)
{
	if(NULL!=(_recv = fopen(_RECV_,"r")))
	{
		fread(buffer,1,_SIZE_,_recv);
		str=buffer;
		fclose(_recv);
		memset(buffer,0,_SIZE_);
	}
}

int main()
{
	std::string hello="哈哈";
	send(hello);
	
	std::string stop="stop";
	std::string getstop;
	
	recv(getstop);
	
	if(0==stop.compare(getstop))
	   std::cout << "结束运行" ;
}

#读取管道

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define _SEND_ "/data/user/0/person.wangchen11.xqceditor/files/.ashmem1"
#define _RECV_ "/data/user/0/person.wangchen11.xqceditor/files/.ashmem0"

#define _SIZE_ 100

void clearBuff(std::stringstream&sstream){
	sstream.str("");//清空buffer
	sstream.clear();//初始化流
}

FILE*_send;
FILE*_recv;

void send(std::string&str)
{
	if(NULL!=(_send = fopen(_SEND_,"w")))
	{
		fwrite(str.c_str(),1,str.length(),_send);
		fclose(_send);
	}
}

char buffer[_SIZE_];

void recv(std::string&str)
{
	if(NULL!=(_recv = fopen(_RECV_,"r")))
	{
		fread(buffer,1,_SIZE_,_recv);
		str=buffer;
		fclose(_recv);
		memset(buffer,0,_SIZE_);
	}
}

int main()
{
	std::string hello;
	recv(hello);
	std::cout << hello;
	
	std::string stop="stop";
	send(stop);
}

##PS:根据以上代码稍作修改,可进行不同应用不同进程之间的高效通讯,类似tcp协议的双项通讯。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号