【转】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. 文件映射 注:文件映射是在多 ...
随机推荐
- abap table control里面各种属性和事件的写法
SAP中,Table Control是在Screen中用的最广泛的控件之一了,可以实现对多行数据的编辑. 简单来说,Table Control是一组屏幕元素在Screen上的重复出现,这就是它与普通屏 ...
- ios 适配问题
两张图解决
- Eclipse中各图标含义
Eclipse中定义很多小图标,在平时的开发工作中,熟悉这些小图标还是很有意义的.那具体意义大家又知道多少呢? 首先,通过在搜索“eclipse icon meaning”,找到了一个比较有用的链接, ...
- GYM 101933A(dp)
要点 \(\sum{w_i} <= 1e8\)是有意味的. 设\(dp[i]\)为至少可以承受重量\(i\)的最大可达高度.转移时可以转移的\(j\)必须满足加上它之后得保证各层不能超重,所以\ ...
- Net Core开源日志框架
Net Core开源日志框架 Exceptionless - .Net Core开源日志框架 作者:markjiang7m2原文地址:https://www.cnblogs.com/markjiang ...
- Java中常见的坑
概述 Java是门极简风格的语言,比其它语言相比,它故意保持较少的特性,不仅在有些不常见的情况下会出些奇奇怪怪的错误,即使很一般的情况下也有可能让人栽根头.如果你习惯了别的语言,你读Java 的代码很 ...
- eclipse查看jar包源文件
话不多说上链接 https://www.cnblogs.com/1995hxt/p/5252098.html这里介绍了完整的流程,亲自试过,可以的! 以防以后要用的时候找不到文件的下载地址,所以就先在 ...
- 第十五章 提升用户体验 之 设计实现MVC controllers 和 actions
1. 概述 controllers 和 actions 是 ASP.NET MVC4中非常重要的组成部分. controller管理用户和程序间的交互,使用action作为完成任务的方式. 如果是包含 ...
- VirtualBox中出现UUID have already exists ,并且数字键盘numlock效果相反
原文地址:https://www.cnblogs.com/xqzt/p/5053338.html 原因:由于linux密码登录错误,修改也报错误,所以只能重新安装虚拟机并在其中安装镜像文件,但是安装镜 ...
- sql常用操作(三)多表查询
1 连接查询 1.1连接就是指两个或2个以上的表(数据源)“连接起来成为一个数据源”. 实际上,两个表的完全的连接是这样的一个过程: 左边的表的每一行,跟右边的表的每一行,两两互相“横向对接”后所得到 ...