C 管道通訊類封裝

2021-10-02 02:58:23 字數 2261 閱讀 9353

// stdafx.h : 標準系統包含檔案的包含檔案,

// 或是經常使用但不常更改的

// 特定於專案的包含檔案

//#pragma once

#include

"targetver.h"

#include

#include

#include

#include

#include

#include

// todo: 在此處引用程式需要的其他標頭檔案

#include

"stdafx.h"

enum pipeusertype

;//定義管道通訊類

class

pipeipc

;

#include

"stdafx.h"

#include

"pipedemo.h"

using

namespace std;

//實現

pipeipc::

pipeipc()

pipeipc::

~pipeipc()

bool pipeipc::

initpipeipc

(std::wstring pipe_name,pipeusertype states)

cout <<

"初始化服務端pipe成功!"

<< endl;

}else

cout <<

"初始化客戶端pipe成功!"

<< endl;

}return

true;}

//讀取資料

bool pipeipc::

readdata

(std::string& datas);if

(!readfile

(m_hpipehandle,szbuffer,

4096

,&rlen,

nullptr))

cout <<

"讀取資料為:"

<< szbuffer << endl;

datas = szbuffer;

return

true;}

//寫入資料

bool pipeipc::

writedata

(std::string datas)

}//連線成功!

dword wlen;

//讀取資料

writefile

(m_hpipehandle,datas.

c_str()

,strlen

(datas.

c_str()

),&wlen,

null);

return

true

;}

unique_ptr

m_p(

newpipeipc()

);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

m_p(

newpipeipc()

);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;

C 命名管道通訊

命名管道 namedpipe 是伺服器程序和乙個或多個客戶程序之間通訊的單向或雙向管道。不同於匿名管道的是 命名管道可以在不相關的程序之間和不同計算機之間使用,伺服器建立命名管道時給它指定乙個名字,任何程序都可以通過該名字開啟管道的另一端,根據給定的許可權和伺服器程序通訊。命名管道提供了相對簡單的程...

C 命名管道通訊

原文 c 命名管道通訊 最近專案中要用c 程序間通訊,以前常見的方法包括rmi 發訊息等。但在windows下面發訊息需要有視窗,我們的程式是乙個後台執行程式,發訊息不試用。rmi又用的太多了,準備用管道通訊來做訊息通訊。管道通訊以前在大學學過,包括匿名管道和命名管道。匿名管道只能用在父子程序之間 ...

C 程序間通訊 有名管道,無名管道

1 管道的概念 管道是單向的 先進先出 的,它把乙個程序的輸出和另乙個程序的輸入連線在一起。乙個程序 寫程序 在管道的 尾部寫入 資料,另乙個程序 讀程序 從管道的 頭部讀出 資料。資料被乙個程序讀出後,將被從管道中刪除,其它讀程序將不能再讀到這些資料。管道提供了簡單的流控制機制,程序試圖讀空管道時...