【转】Windows 邮件槽(MailSlot)
- //Server.cpp
- //服务器邮件槽,用于接收客户发送的广播信息
- #include <windows.h>
- #include <stdio.h>
- int main()
- {
- HANDLE Mailslot;
- char buffer[256];
- DWORD NumberOfBytesRead;
- //Create the mailslot
- Mailslot = CreateMailslot("////.//Mailslot//Myslot",0,
- MAILSLOT_WAIT_FOREVER,NULL);
- if (INVALID_HANDLE_VALUE == Mailslot)
- {
- printf("Failed to create a mailslot %d/n", GetLastError());
- return -1;
- }
- //Read data from the mailslot forever!
- while (0 != ReadFile(Mailslot, buffer, 256, &NumberOfBytesRead, NULL))
- {
- printf("%.*s/n",NumberOfBytesRead,buffer);
- }
- CloseHandle(Mailslot);
- return 0;
- }
客户机:
- //Client.cpp
- //客户端用于发送广播数据到服务器
- #include <windows.h>
- #include <stdio.h>
- int main(int argc, char *argv[])
- {
- HANDLE Mailslot;
- DWORD BytesWritten;
- CHAR ServerName[256];
- //从命令行接受要发送数据到的服务器名
- if (argc < 2)
- {
- printf("Usage: client <server name>/n");
- return -1;
- }
- sprintf(ServerName, "////%s//Mailslot//Myslot",argv[1]);
- Mailslot = CreateFile(ServerName, GENERIC_WRITE,
- FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
- if (INVALID_HANDLE_VALUE == Mailslot)
- {
- printf("WriteFile failed with error %d/n",GetLastError());
- return -1;
- }
- if(0 == WriteFile(Mailslot, "This is a test", 14, &BytesWritten,NULL))
- {
- printf("WriteFile failed with error %d/n", GetLastError());
- return -1;
- }
- printf("Wrote %d byteds/n", BytesWritten);
- CloseHandle(Mailslot);
- return 0;
- }
服务器其它API
- //Server2.cpp
- //非阻塞服务器邮件槽,用于接收客户发送的广播信息
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- BOOL StopProcessing;
- DWORD WINAPI ServeMailslot(LPVOID lpParameter);
- void SendMessageToMailslot(void);
- int main()
- {
- DWORD ThreadId;
- HANDLE MailslotThread;
- StopProcessing = FALSE;
- MailslotThread = CreateThread(NULL,0,ServeMailslot,
- NULL,0,&ThreadId);
- printf("Press a key to stop the server/n");
- _getch();
- //Mark the StopProcessing flag to TRUE so that when ReadFile
- //break, our server thread will end
- StopProcessing = TRUE;
- //Send a message to our mailslot to break the ReadFile call
- //in our server
- SendMessageToMailslot();
- //等待服务线程完成
- if (WAIT_FAILED == WaitForSingleObject(MailslotThread, INFINITE))
- {
- printf("WaitForSingleObject failed with error %d/n",GetLastError());
- return -1;
- }
- return 0;
- }
- //This function is the mailslot server worker function to
- //process all incoming mailslot I/O
- DWORD WINAPI ServeMailslot(LPVOID lpParameter)
- {
- char buffer[2048];
- DWORD NumberOfBytesRead;
- DWORD Ret;
- HANDLE Mailslot;
- Mailslot = CreateMailslot("////.//mailslot//myslot",2048,MAILSLOT_WAIT_FOREVER,NULL);
- if (INVALID_HANDLE_VALUE == Mailslot)
- {
- printf("Failed to create a MailSlot %d/n",GetLastError());
- return -1;
- }
- while (0 != (Ret = ReadFile(Mailslot,buffer,2048,&NumberOfBytesRead,NULL)))
- {
- if (StopProcessing)
- break;
- printf("Received %d bytes/n", NumberOfBytesRead);
- }
- CloseHandle(Mailslot);
- return 0;
- }
- //The SendMessageToMailslot function is designed to send a
- //simple message to our server so we can break the blocking
- //ReadFile API call
- void SendMessageToMailslot()
- {
- HANDLE Mailslot;
- DWORD BytesWritten;
- Mailslot = CreateFile("////.//mailslot//myslot",GENERIC_WRITE,
- FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if (INVALID_HANDLE_VALUE == Mailslot)
- {
- printf("CreateFile failed with error %d/n", GetLastError());
- return;
- }
- if (0 == WriteFile(Mailslot, "STOP", 4, &BytesWritten, NULL))
- {
- printf("WriteFile failed with error %d/n", GetLastError());
- return;
- }
- CloseHandle(Mailslot);
- }
【转】Windows 邮件槽(MailSlot)的更多相关文章
- windows 邮槽mailslot 在服务程序内建立后客户端无权限访问(GetLastError() == 5)的问题
邮槽创建在服务程序内,可以创建成功, 但外部客户端连接时 m_hMailslot = CreateFile("\\\\.\\mailslot\\zdpMailslot",GENER ...
- [Win]进程间通信——邮槽Mailslot
进程间通信 进程的地址空间是私有的.出于安全性的目的,如果一个进程不具有特殊的权限,是无法访问另外一个进程的内存空间的,也无法知道内存中保存的数据的意义.但是在一些具体的应用情况下需要多个进行相互配合 ...
- 【IPC进程间通讯之中的一个】邮槽MailSlot
IPC进程间通信+邮槽MailSlot IPC(Inter-Process Communication.进程间通信). 现代计算机採用虚拟内存机制,为进程提 ...
- Windows邮件客户端
换回WIndows 因为要保存邮件 所以需要邮件客户端 Foxmail 腾讯自家 同样需要独立密码 之前的foxmail是全拼 新注册了一个@qq 发现新注册的@qq绑定到的是新的QQ号 算了 去用1 ...
- Windows进程间通信(中)
二.文件映射 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/O操作,只需简单的指针操作就可读取和修改文件的内容. W ...
- 【转】进程间通信方式总结(windows 和linux)
平时看的书很多,了解的也很多,但不喜欢总结,这不昨天面试的时候被问到了进程间通信的方式,因为没有认真总结过,所以昨天答得不是特别好.现在将linux和windows的进程间通信方式好好总结一下. ...
- 内核对象 windows操作系统
问题: 什么是内核对象? 答:内核对象实际上时由内核分配的一块内存,而且只能由内核来访问.它时一个数据结构,成员包含有关于该对象的信息.一些成员对于所有对象类型都是一样的,比如对象名称.安全描述.使用 ...
- 《Windows核心编程》读书笔记 上
[C++]<Windows核心编程>读书笔记 这篇笔记是我在读<Windows核心编程>第5版时做的记录和总结(部分章节是第4版的书),没有摘抄原句,包含了很多我个人的思考和对 ...
- Windows进程间的通信
一.进程与进程通信 进程间通信(Interprocess Communication, IPC)是指不同的进程之间进行数据共享和数据交换. 二.进程间通信方式 1. 文件映射 注:文件映射是在多 ...
随机推荐
- [转]监控常用TCODE
1 系统监视 1.1 进程监视 SM66/SM50 进程查看 管理员需全天监看系统的进程.长时间运行的后台工作,有缺陷的报表程序,若不进行控制都将消耗掉大量的系统资源.管理员用这个事务码检查他们的环 ...
- java基础第十一篇之Date、Math、自动装箱和拆箱
Date类 表示一个瞬间,就是一个时刻 * * 构造方法: * public Date();//创建一个表示当前系统时间的Date对象 * public Date(long time);//毫秒值,距 ...
- python——基本数据类型1——简介
列表 列表是可变数据类型.是序列类型; 列表的内容可以是数字,字符串和其它列表: 0第一个元素,-1最后一个元素, 定义连续列表 li = list(range(1,10,2)) 列表取值: 取 b: ...
- ES6入门教程---变量和常量
ES6提出了两个新的声明变量的命令:let 和 const 1. 建议不再使用var,而使用let 和const .优先使用const. 在定义之后值是固定不变的,即为常量 常量的值不能修改,但是如果 ...
- Python模块之re
re模块 准备: flags有很多可选值: re.I(IGNORECASE)忽略大小写,括号内是完整的写法 re.M(MULTILINE)多行模式,改变^和$的行为 re.S(DOTALL)点可以匹配 ...
- MySQL表完整性约束
=======MySQL表完整性约束====== 目录: 一.介绍 二.not null 与 default 三.unique 四.primary key 五.auto_increment 六.for ...
- (转)Linux日志管理+ last lastlog lastb
Linux日志管理+ last lastlog lastb 原文:http://blog.csdn.net/xin_y/article/details/53440707 日志管理 日志通常存放在 /v ...
- Spark Mllib里如何程序输出数据集的条数(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集
- springboot+shiro+cas实现单点登录之shiro端搭建
github:https://github.com/peterowang/shiro-cas 本文如有配置问题,请查看之前的springboot集成shiro的文章 1.配置ehcache缓存,在re ...
- Windows中将nginx添加到服务
下载安装nginx http://nginx.org/en/download.html 下载后解压到C盘 C:\nginx-1.14.0 添加服务 需要借助"Windows Service ...