C++管道通信类封装

  • 简单的介绍一下,管道通信类 指的是 IPC通信中的一种,即两个不同的进程之间的通信
  • 现在实现一下管道通信类,方便以后调用使用
  • 头文件(引用相关的库)
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <iostream>
#include <Windows.h>
#include <string>

#include <memory>
// TODO: 在此处引用程序需要的其他头文件

  • 管道通信类的声明
#include "stdafx.h"

enum PIPEUSERTYPE{
	USER_CLIENT, //客户端
	USER_SERVER, //服务端
};

//定义管道通信类
class PipeIPC{
public:
	PipeIPC();
	~PipeIPC();

	bool InitPipeIPC(std::wstring pipe_name,PIPEUSERTYPE states); //初始化管道通信

	//读写数据
	bool WriteData(std::string datas); //写入数据
	bool ReadData(std::string& datas); //读取数据

private:
	std::wstring m_pipe_name;
	HANDLE m_hPipeHandle;
	PIPEUSERTYPE m_state; //状态设置
};
  • 类的声明也很简单,主要实现了写入数据和读出数据 这两个操作,至少写入数据的格式之类的,其实都随便=== 理解原理就很简单
  • 接下来是实现
#include "stdafx.h"
#include "PipeDemo.h"
using namespace std;

//实现
PipeIPC::PipeIPC()
{
	m_pipe_name = L""; //设置空
	m_hPipeHandle = nullptr;
	m_state = USER_CLIENT; //默认为客户端
}

PipeIPC::~PipeIPC()
{
	DisconnectNamedPipe(m_hPipeHandle);
	CloseHandle(m_hPipeHandle);//关闭句柄
}

bool PipeIPC::InitPipeIPC(std::wstring pipe_name,PIPEUSERTYPE states)
{
	m_state = states;
	std::wstring spice_name = L"\\\\.\\pipe\\";//需要拼接的字符串
	spice_name.append(pipe_name); //添加
	m_pipe_name.append(spice_name);
	if (m_state == USER_SERVER)
	{
		m_hPipeHandle = CreateNamedPipe(m_pipe_name.c_str(),PIPE_ACCESS_DUPLEX, //双向模式
			PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT,
			PIPE_UNLIMITED_INSTANCES,//设置最大容纳实例数目
			0,
			0,
			NULL,nullptr);
		//如果管道创建失败
		if (m_hPipeHandle == INVALID_HANDLE_VALUE)
		{
			cout << "" << endl;
			return false;
		}
		//下载成功
		cout << "初始化服务端PIPE成功!" << endl;
	}
	else
	{
		m_hPipeHandle = CreateFile(

			m_pipe_name.c_str(), //创建或打开的对象(管道)名称

			GENERIC_READ | //对象的访问方式:读访问

			GENERIC_WRITE, //对象的访问方式:写访问

			0, //对象是否共享:0表示不共享

			NULL, //指向一个SECURITY_ATTRIBUTES结构的指针

			OPEN_EXISTING, //对象的创建方式:OPEN_EXISTING表示打开对象(管道)

			FILE_ATTRIBUTE_NORMAL, //设置对象的属性和标志

			NULL);
		//如果管道创建失败
		if (m_hPipeHandle == INVALID_HANDLE_VALUE)
		{
			return false;
		}
		//下载成功
		cout << "初始化客户端PIPE成功!" << endl;
	}

	return true;
}

//读取数据
bool PipeIPC::ReadData(std::string& datas)
{
	DWORD rLen = 0;//定义数据长度
	//读取时护具
	char szBuffer[4096] = {0};
	if (!ReadFile(m_hPipeHandle,szBuffer,4096,&rLen,nullptr))
	{
		cout << "读取失败" << endl;
	}
	cout << "读取数据为:" << szBuffer << endl;
	datas = szBuffer;
	return true;
}

//写入数据
bool PipeIPC::WriteData(std::string datas)
{
	//假如是服务端的情况 才需要等待连接
	if (m_state == USER_SERVER)
	{
		if (!ConnectNamedPipe(m_hPipeHandle,NULL)) //等待客户端连接
		{
			cout << "客户端还没连接" << endl;
			//连接失败
			return false;
		}
	}
	//连接成功!
	DWORD wLen;
	//读取数据
	WriteFile(m_hPipeHandle,datas.c_str(),strlen(datas.c_str()),&wLen,NULL);
    return true;
}
  • 调用方法(针对于服务端)
	unique_ptr<PipeIPC> m_p(new PipeIPC());
	std::wstring Names = L"NAMEs11s";
	std::string datas = "ddsss";
	m_p->InitPipeIPC(Names,USER_SERVER);
	m_p->WriteData(datas);
	m_p->ReadData(datas);
	system("pause");
  • 调用方法(针对于客户端)
	unique_ptr<PipeIPC> m_p(new PipeIPC());
	std::wstring Names = L"NAMEs11s";
	std::string datas = "ddsss";
	m_p->InitPipeIPC(Names,USER_CLIENT);
	m_p->ReadData(datas);
	std::string datasss = "ceshi";
	m_p->WriteData(datasss);
	system("pause");
	return 0;
  • 运行结果截图:
    在这里插入图片描述
发布了365 篇原创文章 · 获赞 80 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/Giser_D/article/details/103936242