【转】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. 文件映射 注:文件映射是在多 ...
随机推荐
- angularJs 指令调用父controller某个方法
1.父级controller:例如有个 init() 方法; 父级与子级的通信数据是$scope.controlFlag={}; 那么可以在父级controller里这样写:$scope.contro ...
- 福昕阅读器把pdf某一页保存出来
第一步: 第二步: 第三步: 第四步:点击保存即可
- [Android]XML和JSON的区别
1.定义介绍 (1).XML定义 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允 ...
- Netty-promise
public class TimeEncoder extends ChannelOutboundHandlerAdapter { @Override public void write(Channel ...
- python 1 学习廖雪峰博客
输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...
- DevExpress GridControl 控件二表连动
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 记一次NegativeArraySizeException
问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException 分析:这种异常返回的原因,一般情况下没有报文提示为返回空报文,初步分析是响应报文流长 ...
- bjut193E 吃饭时的怪癖
题目: http://bjutacm.openjudge.cn/lianxi/193E/ 思路: n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数. 实现: #includ ...
- Appium基础一:Appium概念
1.Appium介绍: Appium是一款开源跨平台(IOS和Android平台)支持多种开发语言(java.python等)进行测试Native/Web/Hybrid的Android/iOS App ...
- Word通配符
通配符模式下: ^13表示回车,^32表示空格 第一步,使用通配符替换掉无关文本 M?G-C??[A-Z]{1,20}_[A-Z]{1,20}_201?????_?? VirtualTrial[0-9 ...