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

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

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

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

 

一 管道简介

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

二 实现代码

Server Code:

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

Clietn Code:

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


下一篇:toString() toArray() 等to方法