mailslot怎么用
来源:互联网 发布:win7仿mac dock 编辑:程序博客网 时间:2024/06/02 19:46
使用邮槽通信的进程分为服务端和客户端。
1 邮槽由服务端创建。
2 客户端可以通过邮槽名打开。
3 邮槽通信是单向通信。客写服读
4 一条消息不能大于424byte
5 能进行机内和机间通信
以下编译后先运行server后client
#include#include #include #include HANDLE hSlot;LPTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");BOOL ReadSlot() { DWORD cbMessage, cMessage, cbRead; BOOL fResult; LPTSTR lpszBuffer; TCHAR achID[80]; DWORD cAllMessages; HANDLE hEvent; OVERLAPPED ov; cbMessage = cMessage = cbRead = 0; hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot")); if( NULL == hEvent ) return FALSE; ov.Offset = 0; ov.OffsetHigh = 0; ov.hEvent = hEvent; fResult = GetMailslotInfo( hSlot, // mailslot handle (LPDWORD) NULL, // no maximum message size &cbMessage, // size of next message &cMessage, // number of messages (LPDWORD) NULL); // no read time-out if (!fResult) { printf("GetMailslotInfo failed with %d.\n", GetLastError()); return FALSE; } if (cbMessage == MAILSLOT_NO_MESSAGE) { printf("Waiting for a message...\n"); return TRUE; } cAllMessages = cMessage; while (cMessage != 0) // retrieve all messages { // Create a message-number string. StringCchPrintf((LPTSTR) achID, 80, TEXT("\nMessage #%d of %d\n"), cAllMessages - cMessage + 1, cAllMessages); // Allocate memory for the message. lpszBuffer = (LPTSTR) GlobalAlloc(GPTR, lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage); if( NULL == lpszBuffer ) return FALSE; lpszBuffer[0] = '\0'; fResult = ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, &ov); if (!fResult) { printf("ReadFile failed with %d.\n", GetLastError()); GlobalFree((HGLOBAL) lpszBuffer); return FALSE; } // Concatenate the message and the message-number string. StringCbCat(lpszBuffer, lstrlen((LPTSTR) achID)*sizeof(TCHAR)+cbMessage, (LPTSTR) achID); // Display the message. _tprintf(TEXT("Contents of the mailslot: %s\n"), lpszBuffer); GlobalFree((HGLOBAL) lpszBuffer); fResult = GetMailslotInfo(hSlot, // mailslot handle (LPDWORD) NULL, // no maximum message size &cbMessage, // size of next message &cMessage, // number of messages (LPDWORD) NULL); // no read time-out if (!fResult) { printf("GetMailslotInfo failed (%d)\n", GetLastError()); return FALSE; } } CloseHandle(hEvent); return TRUE; }BOOL WINAPI MakeSlot(LPTSTR lpszSlotName) { hSlot = CreateMailslot(lpszSlotName, 0, // no maximum message size MAILSLOT_WAIT_FOREVER, // no time-out for operations (LPSECURITY_ATTRIBUTES) NULL); // default security if (hSlot == INVALID_HANDLE_VALUE) { printf("CreateMailslot failed with %d\n", GetLastError()); return FALSE; } return TRUE; }void main(){ MakeSlot(SlotName); while(TRUE) { ReadSlot(); Sleep(3000); }}
#include#include LPTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");BOOL WriteSlot(HANDLE hSlot, LPTSTR lpszMessage){ BOOL fResult; DWORD cbWritten; fResult = WriteFile(hSlot, lpszMessage, (DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED) NULL); if (!fResult) { printf("WriteFile failed with %d.\n", GetLastError()); return FALSE; } printf("Slot written to successfully.\n"); return TRUE;}int main(){ HANDLE hFile; hFile = CreateFile(SlotName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d.\n", GetLastError()); return FALSE; } WriteSlot(hFile, TEXT("Message one for mailslot.")); WriteSlot(hFile, TEXT("Message two for mailslot.")); Sleep(5000); WriteSlot(hFile, TEXT("Message three for mailslot.")); CloseHandle(hFile); return TRUE;}
阅读全文
0 0
- mailslot怎么用
- mailslot用法
- Windows邮件槽(Mailslot)
- mailslot又一例
- mailslot又一例
- Windows 邮件槽(MailSlot)
- 邮槽mailslot通信学习
- 命名管道与Mailslot
- 用Mailslot写的客户端和服务器,客户端能向服务器多次发送消息,而服务器可以显示客户端发来的消息
- 【IPC进程间通讯之一】邮槽MailSlot
- 有关MailSlot. msdn windows base services->interprocess communications
- MS Windows Mailslot Ring0 Memory Corruption Exploit (MS06-035)
- [备忘]windows 下 mailslot 接收不到的问题
- VC实现进程间通信(MailSlot附实例)
- [Win]进程间通信——邮槽Mailslot
- [Win]进程间通信——邮槽Mailslot
- 基于Mailslot(邮槽)技术的日志服务程序的实现
- Mailslot,利用邮槽实现进程间通信
- UnicodeEncodeError: 'gbk' codec can't encode character '\xc4' 这类问题之解决
- react-native组件避免重复渲染
- 单列模式
- linux下的文件属性和文件权限
- C语言初步-第24讲: if语句的嵌套(全正整数后再计算)
- mailslot怎么用
- PAT (Advanced) 1012. The Best Rank (25)
- 1024. Palindromic Number (25)
- 相关链表的代码
- CSS 行高、超级链接、background总结
- Spring 基于注解DI的注入
- acm有用吗?
- 使用更新回调更改模型
- UIImageView 怎么添加一个单击手势