基于visual c++之windows核心编程代码分析(16)使用邮槽进行进程通信
来源:互联网 发布:socket客户端接收数据 编辑:程序博客网 时间:2024/05/17 04:58
在Windows环境下,实现进程间通信的方式有很多种,如套接字、管道、远程过程调用和NETBIOS等,邮槽是其中实现单通道进程间通信的一种。创建邮槽的进程被称为邮槽服务器,而其它发送消息给邮槽的进程被称为邮槽客户端。邮槽客户端能发送消息给本机的邮槽,也可发送消息给局域网内其他计算机内的邮槽,所有这些消息都存储在邮槽内,直到邮槽服务器读取它。这些消息通常是以广播的方式发送,建立在面向无链接的数据报的基础上,因此在线路不好时传输质量不可靠。
这种进程间通信的方式比较适用于在局域网环境内传送和接收短消息,也可在局域网内向所有计算机广播消息。
用邮槽进行进程间通信主要通过三个步骤来实现:创建邮槽服务器,向邮槽发送消息和从邮槽中读取消息。下面将用Windows的API函数来完成这三个步骤。
邮槽的客户端代码实现如下。
-
- #include <windows.h>
- #include <stdio.h>
-
- HANDLE hSlot;
- LPTSTR lpszSlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");
- LPTSTR lpszMessage = TEXT("Test Message for mailslot ");
-
-
-
-
-
- void main()
- {
- BOOL fResult;
- HANDLE hFile;
- DWORD cbWritten;
-
- DWORD cbMessage;
-
- hFile = CreateFile(lpszSlotName,
- 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 ;
- }
-
- fResult = WriteFile(hFile,
- lpszMessage,
- (DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR),
- &cbWritten,
- (LPOVERLAPPED) NULL);
-
- if (!fResult)
- {
- printf("WriteFile failed with %d.\n", GetLastError());
- return ;
- }
-
- printf("Slot written to successfully.\n");
- CloseHandle(hFile);
- return ;
- }
邮槽的服务端代码实现如下。
-
- #include <windows.h>
- #include <stdio.h>
-
- HANDLE hSlot;
- LPTSTR lpszSlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");
- LPTSTR Message = TEXT("Message for mailslot in primary domain.");
-
-
-
-
-
- void main()
- {
- DWORD cbMessage, cMessage, cbRead;
- BOOL fResult;
- LPTSTR lpszBuffer;
- TCHAR achID[80];
- DWORD cAllMessages;
- HANDLE hEvent;
- OVERLAPPED ov;
-
- cbMessage = cMessage = cbRead = 0;
-
- hSlot = CreateMailslot(
- lpszSlotName,
- 0,
- MAILSLOT_WAIT_FOREVER,
- (LPSECURITY_ATTRIBUTES) NULL);
-
- if (hSlot == INVALID_HANDLE_VALUE)
- {
- printf("CreateMailslot failed with %d\n", GetLastError());
- return ;
- }
- else printf("Mailslot created successfully.\n");
-
- while(1)
- {
-
- fResult = GetMailslotInfo(hSlot,
- (LPDWORD) NULL,
- &cbMessage,
- &cMessage,
- (LPDWORD) NULL);
-
- if (!fResult)
- {
- printf("GetMailslotInfo failed with %d.\n", GetLastError());
- return ;
- }
-
- if (cbMessage == MAILSLOT_NO_MESSAGE)
- {
-
- Sleep(20000);
- continue;
- }
-
- cAllMessages = cMessage;
-
- while (cMessage != 0)
- {
-
- wsprintf((LPTSTR) achID,
- "\nMessage #%d of %d\n",
- cAllMessages - cMessage + 1,
- cAllMessages);
-
-
- lpszBuffer = (LPTSTR) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
- lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage);
- if( NULL == lpszBuffer )
- {
- return ;
- }
-
- fResult = ReadFile(hSlot,
- lpszBuffer,
- cbMessage,
- &cbRead,
- NULL);
-
- if (!fResult)
- {
- printf("ReadFile failed with %d.\n", GetLastError());
- GlobalFree((HGLOBAL) lpszBuffer);
- return ;
- }
-
-
- lstrcat(lpszBuffer, (LPTSTR) achID);
- printf("Contents of the mailslot: %s\n", lpszBuffer);
-
- HeapFree(GetProcessHeap(),0,lpszBuffer);
-
- fResult = GetMailslotInfo(hSlot,
- (LPDWORD) NULL,
- &cbMessage,
- &cMessage,
- (LPDWORD) NULL);
-
- if (!fResult)
- {
- printf("GetMailslotInfo failed (%d)\n", GetLastError());
- return ;
- }
- }
- }
- return ;
- }
-
- #include <windows.h>
- #include <stdio.h>
-
- HANDLE hSlot;
- LPTSTR lpszSlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");
- LPTSTR Message = TEXT("Message for mailslot in primary domain.");
-
-
-
-
-
- void main()
- {
- DWORD cbMessage, cMessage, cbRead;
- BOOL fResult;
- LPTSTR lpszBuffer;
- TCHAR achID[80];
- DWORD cAllMessages;
- HANDLE hEvent;
- OVERLAPPED ov;
-
- cbMessage = cMessage = cbRead = 0;
-
- hSlot = CreateMailslot(
- lpszSlotName,
- 0,
- MAILSLOT_WAIT_FOREVER,
- (LPSECURITY_ATTRIBUTES) NULL);
-
- if (hSlot == INVALID_HANDLE_VALUE)
- {
- printf("CreateMailslot failed with %d\n", GetLastError());
- return ;
- }
- else printf("Mailslot created successfully.\n");
-
- while(1)
- {
-
- fResult = GetMailslotInfo(hSlot,
- (LPDWORD) NULL,
- &cbMessage,
- &cMessage,
- (LPDWORD) NULL);
-
- if (!fResult)
- {
- printf("GetMailslotInfo failed with %d.\n", GetLastError());
- return ;
- }
-
- if (cbMessage == MAILSLOT_NO_MESSAGE)
- {
-
- Sleep(20000);
- continue;
- }
-
- cAllMessages = cMessage;
-
- while (cMessage != 0)
- {
-
- wsprintf((LPTSTR) achID,
- "\nMessage #%d of %d\n",
- cAllMessages - cMessage + 1,
- cAllMessages);
-
-
- lpszBuffer = (LPTSTR) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
- lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage);
- if( NULL == lpszBuffer )
- {
- return ;
- }
-
- fResult = ReadFile(hSlot,
- lpszBuffer,
- cbMessage,
- &cbRead,
- NULL);
-
- if (!fResult)
- {
- printf("ReadFile failed with %d.\n", GetLastError());
- GlobalFree((HGLOBAL) lpszBuffer);
- return ;
- }
-
-
- lstrcat(lpszBuffer, (LPTSTR) achID);
- printf("Contents of the mailslot: %s\n", lpszBuffer);
-
- HeapFree(GetProcessHeap(),0,lpszBuffer);
-
- fResult = GetMailslotInfo(hSlot,
- (LPDWORD) NULL,
- &cbMessage,
- &cMessage,
- (LPDWORD) NULL);
-
- if (!fResult)
- {
- printf("GetMailslotInfo failed (%d)\n", GetLastError());
- return ;
- }
- }
- }
- return ;
- }
0 0