可以实现多客户端对一服务端,服务端为客户端提供服务。
其实一服务端对应每一个client pipe都新建立了一个pipe。windows允许建立多个同名pipe
效果:
服务端代码:
#define BUFSIZE 2048unsigned __stdcall MsgProcessThread ( void * pParam){ HANDLE hPipe = (HANDLE)pParam; while(1) { char szBufRecv[1024] = { 0}; DWORD dwReadSize = 0; cout<<"服务端准备读消息.."<
客户端代码
#include "stdafx.h"#includeusing namespace std;#include #include int _tmain(int argc, _TCHAR* argv[]){ HANDLE hPipe = []()->HANDLE { while(1) { HANDLE hPipe = CreateFileA( "\\\\.\\pipe\\namedpipe_td",GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0, NULL); if(hPipe != INVALID_HANDLE_VALUE) { cout<<"open pipe success!"<