windows pipe

来源:互联网 发布:手机淘宝没有一元抢购 编辑:程序博客网 时间:2024/06/14 01:17

管道分为 匿名管道命名管道
1.匿名管道只能在父子进程间进行通信,不能在网络间通信,而且数据传输是单向的,只能一端写,另一端读。
2.命令管道可以在任意进程间通信,通信是双向的,任意一端都可读可写,但是在同一时间只能有一端读、一端写。

每一个 命名管道 都有一个唯一的名字以区分于存在于系统的命名对象列表中的其他命名管道。管道服务器在调用CreateNamedPipe()函数创建命名管道的一个或多个实例时为其指定了名称。对于管道客户机,则是在调用CreateFile()或CallNamedPipe()函数以连接一个命名管道实例时对管道名进行指定。命名管道的命名规范与邮槽有些类似,对其标识也是采用的UNC格式:

\\Server\Pipe\[Path]Name 

其中,第一部分\Server指定了服务器的名字,命名管道服务即在此服务器创建,其字串部分可表示为一个小数点(表示本机)、星号(当前网络字段)、域名或是一个真正的服务;第二部分\Pipe与邮槽的\Mailslot一样是一个不可变化的硬编码字串,以指出该文件是从属于NPFS;第三部分[Path]Name则使应用程序可以唯一定义及标识一个命名管道的名字,而且可以设置多级目录。

服务端使用函数:

CreateNamedPipe(); // 创建管道  ConnectNamedPipe(); // 阻塞,等待客户端连接

客户端使用函数:

CreateFile(); // 打开(连接)管道  

双方共用函数

WriteFile();  ReadFile(); // 阻塞,使用方便  CloseHandle(); // 关闭管道,断开连接

服务器端代码示例:

#include <stdio.h>  #include <windows.h>  #define PIPE_NAME L"\\\\.\\Pipe\\test"  HANDLE g_hPipe = INVALID_HANDLE_VALUE;  int main()  {      char buffer[1024];      DWORD WriteNum;      printf("test server.\n");      g_hPipe = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, \              PIPE_TYPE_BYTE|PIPE_READMODE_BYTE , 1, 0, 0, 1000, NULL);      if(g_hPipe == INVALID_HANDLE_VALUE)      {          printf("Create name pipe failed!\n");          goto out;      }      printf("Wait for connect...\n");      if(ConnectNamedPipe(g_hPipe, NULL) == FALSE)      {          printf("Connect failed!\n");          goto out;      }      printf("Connected.\n");      while(1)      {          scanf("%s", &buffer);          if(WriteFile(g_hPipe, buffer, (DWORD)strlen(buffer), &WriteNum, NULL) == FALSE)          {              printf("Write failed!\n");              break;          }      }  out:      printf("Close pipe.\n");      CloseHandle(g_hPipe);      system("pause");      return 0;  }  

客户端代码示例:

#include <stdio.h>  #include <windows.h>  #define PIPE_NAME L"\\\\.\\Pipe\\test"  HANDLE g_hPipe = INVALID_HANDLE_VALUE;  int main()  {      char buffer[1024];      DWORD ReadNum;      printf("test client.\n");      g_hPipe = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE,               0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);      if (g_hPipe == INVALID_HANDLE_VALUE)      {          printf("Connect pipe failed!\n");          goto out;      }      printf("Connected.\n");      while(1)      {          if(ReadFile(g_hPipe, buffer, sizeof(buffer), &ReadNum, NULL) == FALSE)          {              break;          }          buffer[ReadNum] = 0;          printf("%s\n", buffer);      }  out:      printf("Close pipe.\n");      CloseHandle(g_hPipe);      system("pause");      return 0;  }  
4 0
原创粉丝点击