調用WinApi實現郵槽通訊C/S執行個體

來源:互聯網
上載者:User

Just a test.高手無視.

服務端:

//Code by Pnig0s1992  //Date:2012,3,19  #include <stdio.h>  #include <Windows.h>       VOID UseMailSlot(LPTSTR lpMailSlotName);       int main(int argc,char **argv)  {      LPTSTR lpSlotName = TEXT("\\\\.\\mailslot\\first_slot");      UseMailSlot(lpSlotName);      return 0;  }       VOID UseMailSlot(LPTSTR lpMailSlotName)  {      HANDLE hMailSlot;      BOOL bResult;      DWORD dwMessageSize;      DWORD dwMessageCount;      DWORD dwHasReadBytes;      hMailSlot = CreateMailslot(lpMailSlotName,0,MAILSLOT_WAIT_FOREVER,NULL);      if(hMailSlot == INVALID_HANDLE_VALUE)      {          printf("\nCreate mailslot:%S failed.",lpMailSlotName);          return;      }else    {          printf("\nCreate mailslot successfully.");      }      int iCount = 0;      int index = 0;      while (1)      {          bResult = GetMailslotInfo(hMailSlot,NULL,&dwMessageSize,&dwMessageCount,NULL);          if(!bResult)          {              printf("\nGetMailslotInfo failed with error:%d",GetLastError());              CloseHandle(hMailSlot);              return;          }          if(dwMessageCount == 0)          {              printf("\nNo.%d wait for message.",iCount+1);              iCount++;              Sleep(2000);              continue;          }          while(dwMessageCount != 0)          {              LPTSTR lpMessageBuffer = (LPTSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwMessageSize);              if(lpMessageBuffer == NULL)              {                  printf("\nHeapAlloc failed with error:%d",GetLastError());                  CloseHandle(hMailSlot);                  return;              }              bResult = ReadFile(hMailSlot,lpMessageBuffer,dwMessageSize,&dwHasReadBytes,NULL);              if(!bResult)              {                  printf("\nReadFile failed with error:%d",GetLastError());                  HeapFree(GetProcessHeap(),0,lpMessageBuffer);                  CloseHandle(hMailSlot);                  return;              }              printf("\nReceive No.%d message from client.\nContent:%S",index+1,lpMessageBuffer);              index++;              HeapFree(GetProcessHeap(),0,lpMessageBuffer);              bResult = GetMailslotInfo(hMailSlot,0,&dwMessageSize,&dwMessageCount,NULL);              if(!bResult)              {                  printf("\nGetMailslotInfo failed with error:%d",GetLastError());                  return;              }          }      }      return;  }

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。