windows 进程通讯之管道篇
管道介绍
管道是一段被进程用来通讯的共享内存。创建pipe的进程称为服务端,连接到一个管道的进程叫做客户端。一个进程向管道中写入信息,接着另外一个进程读出其中数据。 下面介绍怎样创建,管理和使用管道。关于管道
有命名和匿名两种管道,相比之下匿名管道所需开销较小。这里用管道这个名词表明它是一个信息通道,概念上来说,一个管道连接两端。单向管道允许一端写另一段读,双向管道允许一个进程既可以读又可以向管道写数据。
匿名管道
匿名管道是个没有名字的单向管道,用来在父进程和子进程间传输数据。匿名管道总是在本机使用的,不可以用在网络上进行通讯。
winodws NT/2000 以后的系统:匿名管道通过使用唯一名字的命名管道来实现。因此,你可以将匿名管道的句柄作为参数传递给需要命名管道句柄的函数。
命名管道
命名管道是一个有名字,单向或者双向的,用在服务端和一个或多个客户端进行通讯。一个命名管道的所有实例共享同样的管道名,但是每个实例都有各自的缓存和句柄,作为一个隔离的通道,让客户端-服务器端进行通讯,这些实例允许客户端同时使用相同名字的管道。
任何进程可以访问命名管道,需要进行安全检查,使得管道为相关或者不相关进程通讯提供了一种简单的方式。命名管道可以用在同一台计算机或者网络计算机之间的进程间通讯。
任何进程既可以作为服务端又可作为客户端,使得段对段通讯成为可能。这里服务端的意思是创建了一个命名管道的进程,客户端是连接到命名管道实例的进程。
管道的使用
相关函数.
| Function | Description |
|---|---|
| CallNamedPipe | Connects to a message-type pipe, writes to and reads from the pipe, and then closes the pipe. |
| ConnectNamedPipe | Enables a named pipe server process to wait for a client process to connect to an instance of a named pipe. |
| CreateNamedPipe | Creates an instance of a named pipe and returns a handle for subsequent pipe operations. |
| CreatePipe | Creates an anonymous pipe. |
| DisconnectNamedPipe | Disconnects the server end of a named pipe instance from a client process. |
| GetNamedPipeHandleState | Retrieves information about a specified named pipe. |
| GetNamedPipeInfo | Retrieves information about the specified named pipe. |
| PeekNamedPipe | Copies data from a named or anonymous pipe into a buffer without removing it from the pipe. |
| SetNamedPipeHandleState | Sets the read mode and the blocking mode of the specified named pipe. |
| TransactNamedPipe | Combines the functions that write a message to and read a message from the specified named pipe into a single network operation. |
| WaitNamedPipe | Waits until either a time-out interval elapses or an instance of the specified named pipe is available for a connection. |
1.创建管道(server)
hPipe = CreateNamedPipe(
"\\\\.\\pipe\\mynamedpipe", // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
PIPE_TIMEOUT, // client time-out
NULL); // no security attribute
2.等待连接(server)
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
3.打开管道(client)
hPipe=CreateFile("\\\\.\\pipe\\mynamedpipe", //pipename
GENERIC_READ | GENERIC_WRITE, //desired access
0, //share mode
NULL, //security
OPEN_EXISTING,
0,
NULL);
4.等待管道可用(client)
WaitNamedPipe("\\\\.\\pipe\\mynamedpipe", //pipe name
20000) //timeout
3.从管道读取数据
// Read client requests from the pipe.
fSuccess = ReadFile(
hPipe, // handle to pipe
chRequest, // buffer to receive data
BUFSIZE, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
4. 向管道写数据
fSuccess = WriteFile(
hPipe, // pipe handle
buf, // message
strlen(buf) + 1, // message length
&cbWritten, // bytes written
NULL); // not overlapped
浙公网安备 33010602011771号