windows命名管道

网友投稿 1247 2022-09-28

windows命名管道

windows命名管道

一.windows管道简介

管道(Pipe)的定义: A pipe is a section of shared memory that processes use for communication. The process that creates a pipe is the pipe server. A process that connects to a pipe is a pipe client. One process writes information to the pipe, then the other process reads the information from the pipe.  也就是说,管道就是一部份共享内存以便进程可以用来相互通信,创建了Pipe内核对象的进程就是一个Pipe Server, 当另一个进程与这个进程创建的Pipe Server连接时,就称为Pipe Client.当一个进程往Piple当中写入信息时,另一个进程便可以从这个Pipe读出这个信息。

There are two types of pipes: anonymous pipes and named pipes. Anonymous pipes require less overhead than named pipes, but offer limited services. The term pipe, as used here, implies that a pipe is used as an information conduit. Conceptually, a pipe has two ends. A one-way pipe allows the process at one end to write to the pipe, and allows the process at the other end to read from the pipe. A two-way (or duplex) pipe allows a process to read and write from its end of the pipe: Pipe分为两种:一个是anonymous pipes(匿名Pipe),另一个是named pipes(命名Pipe), anonymous pipes所需要的开销要比named pipes要来得少,但是缺点是提供的功能也少。 pipe这个术语在这里的意思是指:作为一个提供信息的管道,从概念上来理解,Pipe包含了两个端,一端可以允许进程写入,另一端允许进程读出。两个端都可以让进程读或者写。 1.匿名管道(Anonymous Pipes) An anonymous pipe is an unnamed, one-way pipe that typically transfers data between a parent process and a child process. Anonymous pipes are always local; they cannot be used for communication over a network. 可以允许父进程与子进程之间传输数据。它常常用于本地机器,对于网络内的不同机器之间的通信它并不适合。

2.命名管道(Named Pipes) A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client-server communication. The use of instances enables multiple pipe clients to use the same named pipe simultaneously. 命名Pipe中的两个端可以作为一台pipe服务器与另一台(或多台)pipe客户端通信的工具。所有的pipe实例对象共享Pipe Name,但是,每个Pipe实例却有自己的buffer和handle.另外,Pipe Server与各个Pipe client传输数据所用的管道也是不同的,并不共享。它最大的作用在于可以让多个Pipe Client用这个Pipe Name来同时传送数据。

Any process can access named pipes, subject to security checks, making named pipes an easy form of communication between related or unrelated processes. Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network. 任何进程都可以控制named pipes。并遵照其安全性检查,任何有关联的和不关联的进程都可以通过Named Pipes进行通信,可以在一个网络内或不同网络之间的进程进行通信。

Any process can act as both a server and a client, making peer-to-peer communication possible. As used here, the term pipe server refers to a process that creates a named pipe, and the term pipe client refers to a process that connects to an instance of a named pipe. 进程既可以作为Server端又可以作为Client端,在端对端的通信环境下,术语Pipe Server是指创建Pipe的那个进程,而Pipe Client是指连接Pipe实例的进程。

二.命名管道的实现

服务端: 1.使用API函数CreateFile建立与命名管道的连接。 2.使用API函数WriteFile和ReadFile分别向客户端发送数据或从客户端接收数据。 3.使用API函数CloseHandle关闭打开的命名管道会话。 客户端: 1.使用API函数WaitNamedPipe等待一个命名管道实例供自已使用。 2.使用API函数CreateFile建立与命名管道的连接。 3.使用API函数WriteFile和ReadFile分别向服务器端发送数据或从服务器端接收数据。 4.使用API函数CloseHandle关闭打开的命名管道会话。

三.一个例子

Server:

#include "iostream"#include "windows.h"using namespace std;#define PIPE_NAME L"\\\\.\\Pipe\\test" void main(){ char buffer[1024]; DWORD ReadNum; HANDLE hPipe = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 0, 0, 1000, NULL); if (hPipe == INVALID_HANDLE_VALUE) { cout << "创建命名管道失败!" << endl; CloseHandle(hPipe); return; } if (ConnectNamedPipe(hPipe, NULL) == FALSE) { cout << "与客户机连接失败!" << endl; CloseHandle(hPipe); return; } cout << "与客户机连接成功!" << endl; while (1) { if (ReadFile(hPipe, buffer, 1024, &ReadNum, NULL) == FALSE) { cout << "读取数据失败!" << endl; break; } buffer[ReadNum] = 0; cout << "读取数据:" << buffer << endl; } cout << "关闭管道!" << endl; CloseHandle(hPipe); system("pause");}

Client:

#include "iostream"#include "windows.h"#include "stdio.h"using namespace std;#define PIPE_NAME L"\\\\.\\Pipe\\test"void main(){ char buffer[1024]; DWORD WriteNum; if (WaitNamedPipe(PIPE_NAME, NMPWAIT_WAIT_FOREVER) == FALSE) { cout << "等待命名管道实例失败!" << endl; return; } HANDLE hPipe = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hPipe == INVALID_HANDLE_VALUE) { cout << "创建命名管道失败!" << endl; CloseHandle(hPipe); return; } cout << "与服务器连接成功!" << endl; while (1) { gets(buffer);//等待数据输入 if (WriteFile(hPipe, buffer, strlen(buffer), &WriteNum, NULL) == FALSE) { cout << "数据写入管道失败!" << endl; break; } } cout << "关闭管道!" << endl; CloseHandle(hPipe); system("pause");}

在客户端输入数据,回车发送,服务器端打印接收的数据。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Windows7,程序兼容助手:这个程序可能安装不正确
下一篇:外卖广告大规模深度学习模型工程实践
相关文章

 发表评论

暂时没有评论,来抢沙发吧~