[WinApi]邮槽通信C/S实例

Just a test.高手无视.

服务端:

  
  
  
  
  1. //Code by Pnig0s1992  
  2. //Date:2012,3,19  
  3. #include <stdio.h>  
  4. #include <Windows.h>  
  5.  
  6. VOID UseMailSlot(LPTSTR lpMailSlotName);  
  7.  
  8. int main(int argc,char **argv)  
  9. {  
  10.     LPTSTR lpSlotName = TEXT("\\\\.\\mailslot\\first_slot");  
  11.     UseMailSlot(lpSlotName);  
  12.     return 0;  
  13. }  
  14.  
  15. VOID UseMailSlot(LPTSTR lpMailSlotName)  
  16. {  
  17.     HANDLE hMailSlot;  
  18.     BOOL bResult;  
  19.     DWORD dwMessageSize;  
  20.     DWORD dwMessageCount;  
  21.     DWORD dwHasReadBytes;  
  22.     hMailSlot = CreateMailslot(lpMailSlotName,0,MAILSLOT_WAIT_FOREVER,NULL);  
  23.     if(hMailSlot == INVALID_HANDLE_VALUE)  
  24.     {  
  25.         printf("\nCreate mailslot:%S failed.",lpMailSlotName);  
  26.         return;  
  27.     }else 
  28.     {  
  29.         printf("\nCreate mailslot successfully.");  
  30.     }  
  31.     int iCount = 0;  
  32.     int index = 0;  
  33.     while (1)  
  34.     {  
  35.         bResult = GetMailslotInfo(hMailSlot,NULL,&dwMessageSize,&dwMessageCount,NULL);  
  36.         if(!bResult)  
  37.         {  
  38.             printf("\nGetMailslotInfo failed with error:%d",GetLastError());  
  39.             CloseHandle(hMailSlot);  
  40.             return;  
  41.         }  
  42.         if(dwMessageCount == 0)  
  43.         {  
  44.             printf("\nNo.%d wait for message.",iCount+1);  
  45.             iCount++;  
  46.             Sleep(2000);  
  47.             continue;  
  48.         }  
  49.         while(dwMessageCount != 0)  
  50.         {  
  51.             LPTSTR lpMessageBuffer = (LPTSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwMessageSize);  
  52.             if(lpMessageBuffer == NULL)  
  53.             {  
  54.                 printf("\nHeapAlloc failed with error:%d",GetLastError());  
  55.                 CloseHandle(hMailSlot);  
  56.                 return;  
  57.             }  
  58.             bResult = ReadFile(hMailSlot,lpMessageBuffer,dwMessageSize,&dwHasReadBytes,NULL);  
  59.             if(!bResult)  
  60.             {  
  61.                 printf("\nReadFile failed with error:%d",GetLastError());  
  62.                 HeapFree(GetProcessHeap(),0,lpMessageBuffer);  
  63.                 CloseHandle(hMailSlot);  
  64.                 return;  
  65.             }  
  66.             printf("\nReceive No.%d message from client.\nContent:%S",index+1,lpMessageBuffer);  
  67.             index++;  
  68.             HeapFree(GetProcessHeap(),0,lpMessageBuffer);  
  69.             bResult = GetMailslotInfo(hMailSlot,0,&dwMessageSize,&dwMessageCount,NULL);  
  70.             if(!bResult)  
  71.             {  
  72.                 printf("\nGetMailslotInfo failed with error:%d",GetLastError());  
  73.                 return;  
  74.             }  
  75.         }  
  76.     }  
  77.     return;  
  78. }  

客户端:

  
  
  
  
  1. //Code by Pnig0s1992  
  2. //Date:2012,3,19  
  3. #include <stdio.h>  
  4. #include <Windows.h>  
  5.  
  6. VOID ConnectToMailslot(LPTSTR lpMailslotName,LPTSTR lpMessage);  
  7.  
  8. int main(int argc,char ** argv)  
  9. {  
  10.     LPTSTR lpSlotName = TEXT("\\\\.\\mailslot\\first_slot");  
  11.     LPTSTR lpMessage = TEXT("Test for mailslot communication.");  
  12.     ConnectToMailslot(lpSlotName,lpMessage);  
  13.     return 0;  
  14. }  
  15.  
  16. VOID ConnectToMailslot(LPTSTR lpMailslotName,LPTSTR lpMessage)  
  17. {  
  18.     HANDLE hFile;  
  19.     hFile = CreateFile(lpMailslotName,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);  
  20.     if(hFile == INVALID_HANDLE_VALUE)  
  21.     {  
  22.         printf("\nCreateFile failed with error:%d",GetLastError());  
  23.         return;  
  24.     }  
  25.     BOOL bResult;  
  26.     DWORD dwHasWriteBytes;  
  27.     bResult = WriteFile(hFile,lpMessage,(DWORD)(lstrlen(lpMessage)+1)*sizeof(TCHAR),&dwHasWriteBytes,NULL);  
  28.     if(!bResult)  
  29.     {  
  30.         printf("\nWriteFile failed with error:%d",GetLastError());  
  31.         return;  
  32.     }  
  33.     printf("Send message to %S successfully.",lpMailslotName);  
  34.     return;  

 

你可能感兴趣的:(职场,休闲,进程间通信,邮槽)