一、流程
1. 服务端用CreateNamedPipe创建一个命名管道并使用ConnectNamedPipe等待客户端的连接。
2. 客户端使用WaitNamedPipe连接成功后,用CreateFile打开管道并使用WriteFile向管道中写入一段数据(即向服务端发送消息)。
3. 服务端使用ReadFile从管道中读取数据后(即收到消息)再向管道中写入确认信息表明已经收到客户端传输的数据(即通知客户端已收到)。
4. 客户端收到确认信息后结束,调用CloseHandle关闭管道(该管道是CreateFile打开的)。
5.服务端使用DisconnectNamedPipe和CloseHandle关闭管道。
二、实例
先运行服务器,再运行客户端
服务端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50// ServicePip.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <windows.h> #include <ctime> using namespace std; #define pStrPipeName L"\\.\pipe\NamePipe_MoreWindows" int main() { printf(" 命名管道 服务器n"); printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --nn"); printf("创建命名管道并等待连接n"); HANDLE hPipe = CreateNamedPipe(pStrPipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0); if (ConnectNamedPipe(hPipe, NULL) != NULL)//等待连接。 { printf("连接成功,开始接收数据n"); const int BUFFER_MAX_LEN = 256; char szBuffer[BUFFER_MAX_LEN]; memset(szBuffer, 0, BUFFER_MAX_LEN); DWORD dwLen; //接收客户端发送的数据 ReadFile(hPipe, szBuffer, BUFFER_MAX_LEN, &dwLen, NULL);//读取管道中的内容(管道是一种特殊的文件) printf("接收到数据长度为%d字节n", dwLen); printf("具体数据内容如下:%sn", szBuffer); //确认已收到数据,并发送消息给客户端 printf("向客户端发送已经收到标志n"); strcpy_s(szBuffer, "服务器已经收到");//服务器发送的消息 WriteFile(hPipe, szBuffer, strlen(szBuffer) + 1, &dwLen, NULL); } DisconnectNamedPipe(hPipe); CloseHandle(hPipe);//关闭管道 return 0; }
客户端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <iostream> #include <windows.h> #include <ctime> #include <conio.h> using namespace std; #define BUFSIZE 5 #define pStrPipeName L"\\.\pipe\NamePipe_MoreWindows" int main() { printf(" 命名管道 客户端n"); printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --nn"); printf("按任意键以开始连接命名管道n"); _getch(); printf("开始等待命名管道n"); if (WaitNamedPipe(pStrPipeName, NMPWAIT_WAIT_FOREVER) == FALSE) { printf("Error! 连接命名管道失败n"); return 0; } printf("打开命名管道n"); HANDLE hPipe = CreateFile(pStrPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); printf("向服务端发送数据n"); const int BUFFER_MAX_LEN = 256; char szBuffer[BUFFER_MAX_LEN]; memset(szBuffer, 0, BUFFER_MAX_LEN); DWORD dwLen = 0; //向服务端发送数据 sprintf_s(szBuffer, "进程%d说"%s"", GetCurrentProcessId(), "Hello World!"); WriteFile(hPipe, szBuffer, strlen(szBuffer) + 1, &dwLen, NULL); printf("数据写入完毕共%d字节n", dwLen); //接收服务端发回的数据 ReadFile(hPipe, szBuffer, BUFFER_MAX_LEN, &dwLen, NULL);//读取管道中的内容(管道是一种特殊的文件) printf("接收服务端发来的确认信息长度为%d字节n", dwLen); printf("具体数据内容如下:%sn", szBuffer); CloseHandle(hPipe); system("pause"); return 0; }
参考:
https://blog.csdn.net/morewindows/article/details/7390441
https://blog.csdn.net/MoreWindows/article/details/8260087?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
最后
以上就是包容石头最近收集整理的关于C++管道的例子——服务端与客户端通信的全部内容,更多相关C++管道内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复