C++ 命名管道 IPC


技术:IPC,RPC,Windows General
 主题:Named Pipe,Inter-process Communication
 概要:
 命名管道是一种进程间单工或双工的通信机制。它可以在管道服务器和一个或多个管道客户端间进行。客户端可以位于本机或互联网上的远程计算机。
 PIPE_ACCESS_INBOUND(呼入):
 Client (GENERIC_WRITE) ---> Server (GENERIC_READ)
 PIPE_ACCESS_OUTBOUND(外传):
 Client (GENERIC_READ) <--- Server (GENERIC_WRITE)
 PIPE_ACCESS_DUPLEX(双工):
 Client (GENERIC_READ or GENERIC_WRITE, or both) <-->
 Server (GENERIC_READ and GENERIC_WRITE)
 GENERIC_READ(普通读)
 GENERIC_WRITE(普通写)
 下面的代码示例演示了如何调用CreateNamedPipe来创建一个名称为"\\.\pipe\SamplePipe", 的管道。支持全双工连接。这样客户端和服务端都可以从管道中读写数据。自定义安全选项使得认证的用户才具有对管道的读写权限。当有客户端连接管道时,服务端尝试调用ReadFile从管道中读出客户端的消息,并通过调用WriteFile写入响应消息。
 如何演示:
 1.在VS2008中编译CppNamedPipeClient 和CppNamedPipeServer 两个工程,如果成功你会获得两个可执行文件CppNamedPipeClient.exe 和 CppNamedPipeServer.exe.
 2.运行CppNamedPipeServer.exe。如果管道创建成功,程序会以命令行形式输出以下信息:
 Server:
  The named pipe (\\.\pipe\SamplePipe) is created.
  Waiting for the client's connection...
 3.运行CppNamedPipeClient.exe。如果客户端成功连接到命名管道会输出下列信息:
 Client:
  The named pipe (\\.\pipe\SamplePipe) is connected.
  同时服务器端会输出下面的消息来指示有一个客户端连接到管道
  Server:
  Client is connected.
  4.接下来客户端会尝试写入消息到命名管道,程序输出:
  Client:
  Send 56 bytes to server: "Default request from client"
  当服务端从客户端读取消息后打印出:
  Server:
  Receive 56 bytes from client: "Default request from client"
  接下来,服务端写入一个回应消息到管道。
  Server:
  Send 58 bytes to client: "Default response from server"
  然后客户端收到回应消息输出:
  Client:
  Receive 58 bytes from server: "Default response from server"
  最后断开连接,关闭管道。
  主要代码逻辑:
  1.调用CreateNamedPipe创建一个命名管道,指明管道的名称,方向,传输模式,安全属性等

 

// Create the named pipe.
  hNamedPipe = CreateNamedPipe(
      FULL_PIPE_NAME,            // Pipe name.
      PIPE_ACCESS_DUPLEX,        // The pipe is duplex; both server and
                                  // client processes can read from and
                                  // write to the pipe
      PIPE_TYPE_MESSAGE |        // Message type pipe
      PIPE_READMODE_MESSAGE |    // Message-read mode
      PIPE_WAIT,                  // Blocking mode is enabled
      PIPE_UNLIMITED_INSTANCES,  // Max. instances
      BUFFER_SIZE,                // Output buffer size in bytes
      BUFFER_SIZE,                // Input buffer size in bytes
      NMPWAIT_USE_DEFAULT_WAIT,  // Time-out interval
      pSa                        // Security attributes
      );

在这个事例中管道支持全双工通信。安全属性允许认证用户具有读写管道权限,所有管理员组成员具有对管道的全部权限

//
  //  FUNCTION: CreatePipeSecurity(PSECURITY_ATTRIBUTES *)
  //
  //  PURPOSE: The CreatePipeSecurity function creates and initializes a new
  //  SECURITY_ATTRIBUTES structure to allow Authenticated Users read and
  //  write access to a pipe, and to allow the Administrators group full
  //  access to the pipe.
  //
  //  PARAMETERS:
  //  * ppSa - output a pointer to a SECURITY_ATTRIBUTES structure that allows
  //    Authenticated Users read and write access to a pipe, and allows the
  //    Administrators group full access to the pipe. The structure must be
  //    freed by calling FreePipeSecurity.
  //
  //  RETURN VALUE: Returns TRUE if the function succeeds..
  //
  //  EXAMPLE CALL:
  //
  //    PSECURITY_ATTRIBUTES pSa = NULL;
  //    if (CreatePipeSecurity(&pSa))
  //    {
  //        // Use the security attributes
  //        // ...
  //
  //        FreePipeSecurity(pSa);
  //    }
  //
  BOOL CreatePipeSecurity(PSECURITY_ATTRIBUTES *ppSa)

  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容