windows下命名管道、邮件槽使用学习
来源:互联网 发布:卡西欧750知乎 编辑:程序博客网 时间:2024/06/03 21:58
继续 进程间通讯的学习,命名管道
命名管道例子服务端创建管道 CreateNamedPipe
等待客户端连接 ConnectNamedPipe
- Read 或 Write操作
断开连接 DisconnectNamedPipe
- 关闭句柄
#include <stdio.h>
const DWORD BUFSIZE = 1024;
const DWORD PIPE_TIMEOUT = 5000;
int main()
{
HANDLE hPipe = CreateNamedPipeW(L"\\\\.\\pipe\\Dbzhang800Pipe",
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUFSIZE,
BUFSIZE,
PIPE_TIMEOUT,
NULL);
if (hPipe == INVALID_HANDLE_VALUE) {
return -1;
}
for (;;) {
if (ConnectNamedPipe(hPipe, NULL) || (GetLastError() == ERROR_PIPE_CONNECTED)) {
DWORD cbBytesRead;
char chRequest[BUFSIZE];
bool fSuccess = ReadFile (hPipe, chRequest, BUFSIZE, &cbBytesRead, NULL);
chRequest[cbBytesRead] = '\0';
printf("Data Received: %s\n",chRequest);
if (! fSuccess || cbBytesRead == 0) {
break;
}
DisconnectNamedPipe(hPipe);
} else {
CloseHandle(hPipe);
return -2;
}
}
CloseHandle(hPipe);
return 0;
}客户端
直接使用 CreateFile 连接管道
对消息类型也可以使用 CallNamedPipe
#include <stdio.h>
#include <string.h>
const DWORD BUFSIZE = 1024;
const DWORD PIPE_TIMEOUT = 5000;
int main()
{
HANDLE hFile = CreateFileW(L"\\\\.\\pipe\\Dbzhang800Pipe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hFile == INVALID_HANDLE_VALUE) {
printf("cannot connect to Named Pipe\n" );
} else {
DWORD dwWrite;
char szPipeUpdate[200] = "Data from Named Pipe client";
WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
printf("%i bytes has send", (int)dwWrite);
CloseHandle(hFile);
}
return 0;
}邮件槽
调用 CreateMailslot
#include <stdio.h>
int main()
{
HANDLE hSlot = CreateMailslotW(L"\\\\.\\mailslot\\qt4\\dbzhang800", 0, MAILSLOT_WAIT_FOREVER, NULL);
if (hSlot == INVALID_HANDLE_VALUE) {
printf("CreateMailslot failed with %d\n", (int)GetLastError());
return -1;
} else {
printf("Mailslot created successfully.\n");
while (1) {
DWORD cbMsg = 0;
DWORD cMsg = 0;
if (GetMailslotInfo(hSlot, NULL, &cbMsg, &cMsg, NULL)) {
if (cbMsg == MAILSLOT_NO_MESSAGE) {
//printf("no message\n");
continue;
}
if (cMsg){
printf("has message\n");
char * buf = new char[cbMsg+1];
DWORD cbRead = 0;
ReadFile(hSlot, buf, cbMsg, &cbRead, NULL);
buf[cbMsg] = 0;
printf("------------\n%s %d\n-------------\n", buf, (int)cbRead);
delete [] buf;
}
}
}
}
return 0;
}
客户端实现与前面命名管道一样,使用CreateFile
用 CreatePipe 创建两个进程间的管道
http://msdn.microsoft.com/en-us/library/aa365150%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa365785%28v=VS.85%29.aspx
http://www.codersource.net/win32/named-pipes/createnamedpipe-creating-named-pipe-server.aspx
- windows下命名管道、邮件槽使用学习
- windows下命名管道、邮件槽使用学习
- 命名管道和邮件槽
- windows下使用重叠IO(OVERLAPPE)命名管道服务端
- windows下IPC的命名管道例子
- windows学习历程-IPC之命名管道
- 管道和命名管道(windows)
- windows管道系统 ----命名管道
- Windows 命名管道1
- Windows 命名管道2
- Windows命名管道
- 剪贴板,匿名管道,命名管道,邮槽学习笔记
- 命名管道学习笔记
- Windows上C++使用命名管道进行进程间通讯
- 命名管道的使用
- 命名管道的使用
- linux 使用命名管道
- 使用命名管道
- 从美国云计算的五年发展看中国
- 云计算发展进程中的六大关键技术
- ASP.NET获取客户端的操作系统、浏览器、.NET版本等信息
- 迭代器失效小结
- 10月编程语言排行榜:Dart发力,欲将JS赶下神坛
- windows下命名管道、邮件槽使用学习
- 微软JavaScript团队向Dart语言泼冷水
- HttpApplication事件执行顺序
- 求数组中逆序对的个数
- Dart语言遭微软鄙视 谷歌疏远盟友必败
- 什么是软件设计
- 超简单的日志类
- 揭开OpenStack光鲜外表之下的阴暗
- 解决IE6、IE8 宽度兼容