一、命名管道(Named Pipes)
管道(Pipe)实际是用于进程间通信的一段共享内存,创建管道的进程称为管道服务器,连接到一个管道的进程为管道客户机。一个进程在向管道写入数据后,另一进程就可以从管道的另一端将其读取出来。
匿名管道(Anonymous Pipes)是在父进程和子进程间单向传输数据的一种未命名的管道,只能在本地计算机中使用,而不可用于网络间的通信。
命名管道,顾名思义,这个管道肯定是有名字的。通过管道的名字来确保多个进程访问同一个管道。事实上,命名管道不仅可在同一台计算机的不同进程之间传输数据,甚至能在跨越一个网络的不同计算机的不同进程之间,支持可靠的、单向或双向的数据通信。
重要函数
CreateNamedPipe
函数功能:创建命名管道
HANDLEWINAPICreateNamedPipe(
LPCTSTRlpName,
DWORDdwOpenMode,
DWORDdwPipeMode,
DWORDnMaxInstances,
DWORDnOutBufferSize,
DWORDnInBufferSize,
DWORDnDefaultTimeOut,
LPSECURITY_ATTRIBUTESlpSecurityAttributes
);
ConnectNamedPipe
函数功能:等待客户端连接命名管道
函数原型:
BOOLWINAPIConnectNamedPipe(
HANDLEhNamedPipe,
LPOVERLAPPEDlpOverlapped
);
WaitNamedPipe
函数功能:客户端连接命名管道
函数原型:
BOOLWINAPIWaitNamedPipe(
LPCTSTRlpNamedPipeName,
DWORDnTimeOut
);
二、实例
下面给出使用命名管道的实例,该实例分为命名管道的服务端和客户端。服务端和客户端的主要步骤如下所示:
1. 服务端用CreateNamedPipe创建一个命名管道并使用ConnectNamedPipe等待客户端的连接
2. 客户端使用WaitNamedPipe连接成功后,用CreateFile打开管道
3、服务端使用WriteFile向管道中写入一段数据(即向客户端发送消息)
4、客户端使用ReadFile从管道中读取数据,显示,调用CloseHandle关闭管道(该管道是CreateFile打开的)。
5、服务端使用DisconnectNamedPipe和CloseHandle关闭管道。
服务端代码
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
int main()
{
printf("创建命名管道并等待连接\n");
HANDLE hPipe = CreateNamedPipe(L"\\\\.\\Pipe\\mypipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT
, PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0);
//waiting to be connected
if (ConnectNamedPipe(hPipe, NULL) != NULL)
{
printf("连接成功,开始发送数据\n");
DWORD dwWrite;
const char *pStr = "data from server";
if (!WriteFile(hPipe, pStr, strlen(pStr), &dwWrite, NULL))
{
cout << "write failed..." << endl << endl;
return 0;
}
cout << "sent data: " << endl << pStr << endl << endl;
}
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);//关闭管道
printf("关闭管道\n");
system("pause");
}
客户端代码
// ClientPip.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <windows.h>
#include <ctime>
#include <conio.h>
using namespace std;
#define BUFSIZE 5
int main()
{
printf("命名管道:客户端上线\n");
printf("按任意键以开始连接命名管道\n");
_getch();
printf("开始等待命名管道\n");
if (WaitNamedPipe(L"\\\\.\\Pipe\\mypipe", NMPWAIT_WAIT_FOREVER) == FALSE)
return 0;
printf("打开命名管道\n");
HANDLE hPipe = CreateFile(L"\\\\.\\Pipe\\mypipe", GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if ((long)hPipe == -1)
return 0;
//接收服务端发回的数据
BOOL fSuccess = false;
DWORD len = 0;
char buffer[BUFSIZE];
string recvData = "";
do
{
fSuccess = ReadFile(hPipe, buffer, BUFSIZE * sizeof(char), &len, NULL);
char buffer2[BUFSIZE + 1] = { 0 };
memcpy(buffer2, buffer, len);
recvData.append(buffer2);
if (!fSuccess || len < BUFSIZE)
break;
} while (true);
cout << "recv data:" << endl << recvData.c_str() << endl << endl;
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
system("pause");
return 0;
}
先打开服务器,再打开客户端
参考: