基于visual c++之windows核心编程代码分析(16)使用邮槽进行进程通信

来源:互联网 发布:socket客户端接收数据 编辑:程序博客网 时间:2024/05/17 04:58

在Windows环境下,实现进程间通信的方式有很多种,如套接字、管道、远程过程调用和NETBIOS等,邮槽是其中实现单通道进程间通信的一种。创建邮槽的进程被称为邮槽服务器,而其它发送消息给邮槽的进程被称为邮槽客户端。邮槽客户端能发送消息给本机的邮槽,也可发送消息给局域网内其他计算机内的邮槽,所有这些消息都存储在邮槽内,直到邮槽服务器读取它。这些消息通常是以广播的方式发送,建立在面向无链接的数据报的基础上,因此在线路不好时传输质量不可靠。

  这种进程间通信的方式比较适用于在局域网环境内传送和接收短消息,也可在局域网内向所有计算机广播消息。

  用邮槽进行进程间通信主要通过三个步骤来实现:创建邮槽服务器,向邮槽发送消息和从邮槽中读取消息。下面将用Windows的API函数来完成这三个步骤。

 

邮槽的客户端代码实现如下。

[cpp] view plain copy
  1. /* 头文件 */  
  2. #include <windows.h>  
  3. #include <stdio.h>  
  4. /* 全局变量 */  
  5. HANDLE hSlot;  
  6. LPTSTR lpszSlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");     // mailslot名  
  7. LPTSTR lpszMessage = TEXT("Test Message for mailslot "); // 通信的内容  
  8.   
  9. /* ************************************ 
  10. * void main() 
  11. * 功能    进程间mailslot通信客户端 
  12. **************************************/  
  13. void main()  
  14. {   
  15.     BOOL fResult;   
  16.     HANDLE hFile;   
  17.     DWORD cbWritten;   
  18.   
  19.     DWORD cbMessage;  
  20.     // 打开mailslot  
  21.     hFile = CreateFile(lpszSlotName,   
  22.         GENERIC_WRITE,      // 可写  
  23.         FILE_SHARE_READ,  
  24.         (LPSECURITY_ATTRIBUTES) NULL,   
  25.         OPEN_EXISTING,      // 打开一个已经存在的mailslot,应该由服务端已经创建  
  26.         FILE_ATTRIBUTE_NORMAL,   
  27.         (HANDLE) NULL);   
  28.   
  29.     if (hFile == INVALID_HANDLE_VALUE)   
  30.     {   
  31.         printf("CreateFile failed with %d.\n", GetLastError());   
  32.         return ;   
  33.     }  
  34.     // 向mailslot写入  
  35.     fResult = WriteFile(hFile,   
  36.         lpszMessage,   
  37.         (DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR),    
  38.         &cbWritten,   
  39.         (LPOVERLAPPED) NULL);   
  40.   
  41.     if (!fResult)   
  42.     {   
  43.         printf("WriteFile failed with %d.\n", GetLastError());   
  44.         return ;   
  45.     }   
  46.     // 结束  
  47.     printf("Slot written to successfully.\n");   
  48.     CloseHandle(hFile);   
  49.     return ;  
  50. }  


 

 

邮槽的服务端代码实现如下。

 

[cpp] view plain copy
  1. /* 头文件 */  
  2. #include <windows.h>  
  3. #include <stdio.h>  
  4. /* 全局变量 */  
  5. HANDLE hSlot;  
  6. LPTSTR lpszSlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");  
  7. LPTSTR Message = TEXT("Message for mailslot in primary domain.");   
  8.   
  9. /* ************************************ 
  10. * void main() 
  11. * 功能    进程间mailslot通信客户端 
  12. **************************************/  
  13. void main()  
  14. {   
  15.     DWORD cbMessage, cMessage, cbRead;   
  16.     BOOL fResult;   
  17.     LPTSTR lpszBuffer;   
  18.     TCHAR achID[80];   
  19.     DWORD cAllMessages;   
  20.     HANDLE hEvent;  
  21.     OVERLAPPED ov;  
  22.   
  23.     cbMessage = cMessage = cbRead = 0;   
  24.   
  25.     hSlot = CreateMailslot(  
  26.         lpszSlotName,       // mailslot 名  
  27.         0,                          // 不限制消息大小   
  28.         MAILSLOT_WAIT_FOREVER,         // 无超时   
  29.         (LPSECURITY_ATTRIBUTES) NULL);   
  30.   
  31.     if (hSlot == INVALID_HANDLE_VALUE)   
  32.     {   
  33.         printf("CreateMailslot failed with %d\n", GetLastError());  
  34.         return ;   
  35.     }   
  36.     else printf("Mailslot created successfully.\n");   
  37.   
  38.     while(1)  
  39.     {  
  40.         // 获取mailslot信息  
  41.         fResult = GetMailslotInfo(hSlot, // mailslot 句柄   
  42.             (LPDWORD) NULL,               // 无最大消息限制  
  43.             &cbMessage,                   // 下一条消息的大小  
  44.             &cMessage,                    // 消息的数量  
  45.             (LPDWORD) NULL);              // 无时限  
  46.   
  47.         if (!fResult)   
  48.         {   
  49.             printf("GetMailslotInfo failed with %d.\n", GetLastError());   
  50.             return ;   
  51.         }   
  52.   
  53.         if (cbMessage == MAILSLOT_NO_MESSAGE)   
  54.         {   
  55.             // 没有消息,过一段时间再去读  
  56.             Sleep(20000);  
  57.             continue;  
  58.         }   
  59.   
  60.         cAllMessages = cMessage;   
  61.   
  62.         while (cMessage != 0)  // 获取全部消息,有可能不只一条  
  63.         {   
  64.             // 提示信息  
  65.             wsprintf((LPTSTR) achID,   
  66.                 "\nMessage #%d of %d\n",   
  67.                 cAllMessages - cMessage + 1,   
  68.                 cAllMessages);   
  69.   
  70.             // 分配空间  
  71.             lpszBuffer = (LPTSTR) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,  
  72.                 lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage);   
  73.             if( NULL == lpszBuffer )  
  74.             {  
  75.                 return ;  
  76.             }  
  77.             // 读取消息  
  78.             fResult = ReadFile(hSlot,   // mailslot句柄  
  79.                 lpszBuffer,         // 缓存  
  80.                 cbMessage,          // 消息的长度  
  81.                 &cbRead,            // 实际读取的长度  
  82.                 NULL);   
  83.   
  84.             if (!fResult)   
  85.             {   
  86.                 printf("ReadFile failed with %d.\n", GetLastError());   
  87.                 GlobalFree((HGLOBAL) lpszBuffer);   
  88.                 return ;   
  89.             }   
  90.   
  91.             // 处理信息,显示  
  92.             lstrcat(lpszBuffer, (LPTSTR) achID);   
  93.             printf("Contents of the mailslot: %s\n", lpszBuffer);   
  94.   
  95.             HeapFree(GetProcessHeap(),0,lpszBuffer);   
  96.             // 计算剩余的消息数  
  97.             fResult = GetMailslotInfo(hSlot,    
  98.                 (LPDWORD) NULL,       
  99.                 &cbMessage,                
  100.                 &cMessage,                  
  101.                 (LPDWORD) NULL);             
  102.   
  103.             if (!fResult)   
  104.             {   
  105.                 printf("GetMailslotInfo failed (%d)\n", GetLastError());  
  106.                 return ;   
  107.             }   
  108.         }   
  109.     }  
  110.     return ;   
  111. }  


 


[cpp] view plain copy
  1. /* 头文件 */  
  2. #include <windows.h>  
  3. #include <stdio.h>  
  4. /* 全局变量 */  
  5. HANDLE hSlot;  
  6. LPTSTR lpszSlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");  
  7. LPTSTR Message = TEXT("Message for mailslot in primary domain.");   
  8.   
  9. /* ************************************ 
  10. * void main() 
  11. * 功能    进程间mailslot通信客户端 
  12. **************************************/  
  13. void main()  
  14. {   
  15.     DWORD cbMessage, cMessage, cbRead;   
  16.     BOOL fResult;   
  17.     LPTSTR lpszBuffer;   
  18.     TCHAR achID[80];   
  19.     DWORD cAllMessages;   
  20.     HANDLE hEvent;  
  21.     OVERLAPPED ov;  
  22.   
  23.     cbMessage = cMessage = cbRead = 0;   
  24.   
  25.     hSlot = CreateMailslot(  
  26.         lpszSlotName,       // mailslot 名  
  27.         0,                          // 不限制消息大小   
  28.         MAILSLOT_WAIT_FOREVER,         // 无超时   
  29.         (LPSECURITY_ATTRIBUTES) NULL);   
  30.   
  31.     if (hSlot == INVALID_HANDLE_VALUE)   
  32.     {   
  33.         printf("CreateMailslot failed with %d\n", GetLastError());  
  34.         return ;   
  35.     }   
  36.     else printf("Mailslot created successfully.\n");   
  37.   
  38.     while(1)  
  39.     {  
  40.         // 获取mailslot信息  
  41.         fResult = GetMailslotInfo(hSlot, // mailslot 句柄   
  42.             (LPDWORD) NULL,               // 无最大消息限制  
  43.             &cbMessage,                   // 下一条消息的大小  
  44.             &cMessage,                    // 消息的数量  
  45.             (LPDWORD) NULL);              // 无时限  
  46.   
  47.         if (!fResult)   
  48.         {   
  49.             printf("GetMailslotInfo failed with %d.\n", GetLastError());   
  50.             return ;   
  51.         }   
  52.   
  53.         if (cbMessage == MAILSLOT_NO_MESSAGE)   
  54.         {   
  55.             // 没有消息,过一段时间再去读  
  56.             Sleep(20000);  
  57.             continue;  
  58.         }   
  59.   
  60.         cAllMessages = cMessage;   
  61.   
  62.         while (cMessage != 0)  // 获取全部消息,有可能不只一条  
  63.         {   
  64.             // 提示信息  
  65.             wsprintf((LPTSTR) achID,   
  66.                 "\nMessage #%d of %d\n",   
  67.                 cAllMessages - cMessage + 1,   
  68.                 cAllMessages);   
  69.   
  70.             // 分配空间  
  71.             lpszBuffer = (LPTSTR) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,  
  72.                 lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage);   
  73.             if( NULL == lpszBuffer )  
  74.             {  
  75.                 return ;  
  76.             }  
  77.             // 读取消息  
  78.             fResult = ReadFile(hSlot,   // mailslot句柄  
  79.                 lpszBuffer,         // 缓存  
  80.                 cbMessage,          // 消息的长度  
  81.                 &cbRead,            // 实际读取的长度  
  82.                 NULL);   
  83.   
  84.             if (!fResult)   
  85.             {   
  86.                 printf("ReadFile failed with %d.\n", GetLastError());   
  87.                 GlobalFree((HGLOBAL) lpszBuffer);   
  88.                 return ;   
  89.             }   
  90.   
  91.             // 处理信息,显示  
  92.             lstrcat(lpszBuffer, (LPTSTR) achID);   
  93.             printf("Contents of the mailslot: %s\n", lpszBuffer);   
  94.   
  95.             HeapFree(GetProcessHeap(),0,lpszBuffer);   
  96.             // 计算剩余的消息数  
  97.             fResult = GetMailslotInfo(hSlot,    
  98.                 (LPDWORD) NULL,       
  99.                 &cbMessage,                
  100.                 &cMessage,                  
  101.                 (LPDWORD) NULL);             
  102.   
  103.             if (!fResult)   
  104.             {   
  105.                 printf("GetMailslotInfo failed (%d)\n", GetLastError());  
  106.                 return ;   
  107.             }   
  108.         }   
  109.     }  
  110.     return ;   
  111. }  

0 0