【转】C++ 进程间的通讯(一):简单的有名管道实现

原文: C++ 进程间的通讯(一):简单的有名管道实现

--------------------------------------------------

进程间的通讯(一):简单的有名管道实现

 

一 管道简介

命名管道(Named Pipe)是服务器进程和一个或多个客户进程之间通信的单向或双向管道。不同于匿名管道的是命名管道可以在不相关的进程之间和不同计算机之间使用,服务器建立命名管道时给它指定一个名字,任何进程都可以通过该名字打开管道的另一端,根据给定的权限和服务器进程通信。
其优点是实现起来比较简单方便.
缺点是会使进程之间的代码耦合度增加.并且管道通信只适用于同一台主机上的进程之间通讯.

二 实现代码

Server Code:

[cpp] view plain copy

 

 print?

  1. #include "stdafx.h"  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include <ctime>  
  5.   
  6. int main(int argc, _TCHAR* argv[])  
  7. {  
  8.     srand(time(NULL));  
  9.   
  10.     char buf[256] = "";  
  11.     DWORD rlen = 0;  
  12.     HANDLE hPipe = CreateNamedPipe(  
  13.         TEXT("\\\\.\\Pipe\\mypipe"),                        //管道名  
  14.         PIPE_ACCESS_DUPLEX,                                 //管道类型   
  15.         PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT,  //管道参数  
  16.         PIPE_UNLIMITED_INSTANCES,                           //管道能创建的最大实例数量  
  17.         0,                                                  //输出缓冲区长度 0表示默认  
  18.         0,                                                  //输入缓冲区长度 0表示默认  
  19.         NMPWAIT_WAIT_FOREVER,                               //超时时间  
  20.         NULL);                                                  //指定一个SECURITY_ATTRIBUTES结构,或者传递零值.  
  21.     if (INVALID_HANDLE_VALUE == hPipe)  
  22.     {  
  23.         printf("Create Pipe Error(%d)\n",GetLastError());  
  24.     }  
  25.     else  
  26.     {  
  27.         printf("Waiting For Client Connection...\n");  
  28.         if(ConnectNamedPipe(hPipe, NULL)==NULL) //阻塞等待客户端连接。  
  29.         {  
  30.             printf("Connection failed!\n");  
  31.         }  
  32.         else  
  33.         {  
  34.             printf("Connection Success!\n");  
  35.         }  
  36.   
  37.         while (true)  
  38.         {  
  39.             if(ReadFile(hPipe,buf,256,&rlen,NULL)==FALSE) //接受客户端发送过来的内容  
  40.             {             
  41.                 printf("Read Data From Pipe Failed!\n");  
  42.                 break;  
  43.             }  
  44.             else  
  45.             {  
  46.                 printf("From Client: data = %s, size = %d\n", buf, rlen);  
  47.                 char wbuf[256] = "";  
  48.                 sprintf(wbuf, "%s%d", wbuf, rand()%1000);  
  49.                 DWORD wlen = 0;  
  50.                 WriteFile(hPipe, wbuf, sizeof(wbuf), &wlen, 0); //向客户端发送内容  
  51.                 printf("To Client: data = %s, size = %d\n", wbuf, wlen);  
  52.                 Sleep(1000);  
  53.             }  
  54.         }  
  55.         CloseHandle(hPipe);//关闭管道  
  56.     }  
  57.   
  58.     system("PAUSE");  
  59.     return 0;  
  60. }  

Clietn Code:

[cpp] view plain copy

 

 print?

  1. #include "stdafx.h"  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include <ctime>  
  5.   
  6. int main(int argc, _TCHAR* argv[])  
  7. {  
  8.     srand(time(NULL));  
  9.   
  10.     DWORD wlen = 0;  
  11.     Sleep(1000);//等待pipe的创建成功!  
  12.   
  13.     BOOL bRet = WaitNamedPipe(TEXT("\\\\.\\Pipe\\mypipe"), NMPWAIT_WAIT_FOREVER);  
  14.   
  15.     if (!bRet)  
  16.     {  
  17.         printf("connect the namedPipe failed!\n");  
  18.         return 0;  
  19.     }  
  20.   
  21.     HANDLE hPipe=CreateFile(            //管道属于一种特殊的文件  
  22.         TEXT("\\\\.\\Pipe\\mypipe"),    //创建的文件名  
  23.         GENERIC_READ | GENERIC_WRITE,   //文件模式  
  24.         0,                              //是否共享  
  25.         NULL,                           //指向一个SECURITY_ATTRIBUTES结构的指针  
  26.         OPEN_EXISTING,                  //创建参数  
  27.         FILE_ATTRIBUTE_NORMAL,          //文件属性(隐藏,只读)NORMAL为默认属性  
  28.         NULL);                          //模板创建文件的句柄  
  29.   
  30.     if (INVALID_HANDLE_VALUE == hPipe)  
  31.     {  
  32.         printf("open the exit pipe failed!\n");  
  33.     }  
  34.     else  
  35.     {  
  36.         while(true)  
  37.         {  
  38.             char buf[256] = "";  
  39.             sprintf(buf,"%s%d",buf,rand()%1000);  
  40.             if(WriteFile(hPipe,buf,sizeof(buf),&wlen,0)==FALSE) //向服务器发送内容  
  41.             {  
  42.                 printf("write to pipe failed!\n");  
  43.                 break;  
  44.             }  
  45.             else  
  46.             {  
  47.                 printf("To Server: data = %s, size = %d\n", buf, wlen);  
  48.                 char rbuf[256] = "";  
  49.                 DWORD rlen = 0;  
  50.                 ReadFile(hPipe, rbuf, sizeof(rbuf), &rlen, 0);  //接受服务发送过来的内容  
  51.                 printf("From Server: data = %s, size = %d\n", rbuf, rlen);  
  52.             }  
  53.             Sleep(1000);  
  54.         }  
  55.         CloseHandle(hPipe);//关闭管道  
  56.     }  
  57.   
  58.     system("PAUSE");  
  59.     return 0;  
  60. }  

原文链接: https://www.cnblogs.com/oxspirt/p/6732954.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    【转】C++ 进程间的通讯(一):简单的有名管道实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/252765

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午6:20
下一篇 2023年2月14日 上午6:20

相关推荐