【转】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. 文件映射 注:文件映射是在多 ...
随机推荐
- 了解HTTP协议和TCP协议
HTTP(超文本传输协议),互联网上应用最为广泛的一种网络协议.所有的www文件都必须遵守这个标准.HTTP是一个客户端和服务端请求和应答的标准(TCP):客户通过浏览器发起一个到服务器上指定端口的H ...
- 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (二)
本文属于图神经网络的系列文章,文章目录如下: 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (一) 从图(Graph)到图卷积(Graph Convolutio ...
- day3字符串操作作业详解
1.day3题目 1.有变量name = "aleX leNb" 完成如下操作: 1) 移除 name 变量对应的值两边的空格,并输出处理结果 2) 移除name变量左边的&quo ...
- JMeter(6) jenkins测试报告及邮件优化
jenkins邮件 使用jenkins执行完任务自动将测试结果发送到邮箱,效果如下: 生成html报告 build文件设置 jenkins设置 SummaryReport写入邮件正文 ...
- NOI2015品酒大会 后缀数组
自己尝试敲后缀数组,发现难看(tiao)的不行,于是抄了板子 考虑建出hei以后转化出的问题: 对于一个数组中权值大于等于k的连续部分,求取两个数的方案数和两数积的最大值 (好气啊,可以有负数) 把询 ...
- idea svn操作
https://blog.csdn.net/bug_love/article/details/72875511
- javac 找不到文件 的可能原因
初学Java还不太明白,竟在些简单的事情上栽跟头,分享一下省的麻烦. 当我们配置好JDK和环境变量之后,在命令行下输入javac,说明我们的安装是正确的.
- Unity使用 转载
创建空的ASP.NET MVC3项目,添加对Unity2.0动态库的引用. 方法1:在MSDN上下载Untity2.0,安装后,默认安装在C:\Program Files\Microsoft Unit ...
- Random类、ThreadLocalRandom类
Random和ThreadLocalRandom类均用于生成伪随机数. Random的构造函数: Random() 默认以系统当前时间为种子,相当于Random(System.currentT ...
- Swing编程概述
Swing作为AWT组件的“强化版”,它的产生主要是为了克服AWT构建的GUI,无法在所有平台都通用的问题.允许编程人员跨平台时指定统一的GUI显示风格也是Swing的最大优势.Swing是AWT的补 ...