服務端代碼:
// pipe_server.cpp : 定義控制臺應用程序的入口點。// #include "stdafx.h"#include <stdio.h>#include <windows.h>#include <ctime> int main(int argc, _TCHAR* argv[]){   srand(time(NULL));   char buf[256] = "";   DWORD rlen = 0;   HANDLE hPipe = CreateNamedPipe(     TEXT("http:////.//Pipe//mypipe"),            //管道名     PIPE_ACCESS_DUPLEX,                  //管道類型     PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT,  //管道參數     PIPE_UNLIMITED_INSTANCES,              //管道能創建的最大實例數量     0,                          //輸出緩沖區長度 0表示默認     0,                          //輸入緩沖區長度 0表示默認     NMPWAIT_WAIT_FOREVER,                //超時時間     NULL);                        //指定一個SECURITY_ATTRIBUTES結構,或者傳遞零值   if (INVALID_HANDLE_VALUE == hPipe)   {     printf("Create Pipe Error(%d)/n",GetLastError());   }   else   {     printf("Waiting For Client Connection.../n");     if(!ConnectNamedPipe(hPipe, NULL))  //阻塞等待客戶端連接。     {       printf("Connection failed!/n");     }     else     {       printf("Connection Success!/n");     }     while (true)     {       if(!ReadFile(hPipe,buf,256,&rlen,NULL)) //接受客戶端發送過來的內容       {               printf("Read Data From Pipe Failed!/n");         break;       }       else       {         printf("From Client: data = %s, size = %d/n", buf, rlen);                  char wbuf[256] = "";         sprintf(wbuf, "%s%d", wbuf, rand()%1000);         DWORD wlen = 0;         WriteFile(hPipe, wbuf, sizeof(wbuf), &wlen, 0);  //向客戶端發送內容         printf("To Client: data = %s, size = %d/n", wbuf, wlen);         Sleep(1000);       }     }     FlushFileBuffers(hPipe);     DisconnectNamedPipe(hPipe);     CloseHandle(hPipe);//關閉管道   }   system("pause");   return 0;}客戶端代碼:
// pipe_client.cpp : 定義控制臺應用程序的入口點。// #include "stdafx.h"#include <stdio.h>#include <windows.h>#include <ctime> int main(int argc, _TCHAR* argv[]){   srand(time(NULL));   DWORD wlen = 0;   Sleep(1000);//等待pipe的創建成功!   BOOL bRet = WaitNamedPipe(TEXT("http:////.//Pipe//mypipe"), NMPWAIT_WAIT_FOREVER);   if (!bRet)   {     printf("connect the namedPipe failed!/n");     return 0;   }   HANDLE hPipe = CreateFile(      //管道屬于一種特殊的文件     TEXT("http:////.//Pipe//mypipe"),  //創建的文件名     GENERIC_READ | GENERIC_WRITE,  //文件模式     0,                //是否共享     NULL,              //指向一個SECURITY_ATTRIBUTES結構的指針     OPEN_EXISTING,          //創建參數     FILE_ATTRIBUTE_NORMAL,      //文件屬性(隱藏,只讀)NORMAL為默認屬性     NULL);              //模板創建文件的句柄   if (INVALID_HANDLE_VALUE == hPipe)   {     printf("open the exit pipe failed!/n");   }   else   {     while(true)     {       char buf[256] = "";       sprintf(buf,"%s%d",buf,rand()%1000);       if(WriteFile(hPipe,buf,sizeof(buf),&wlen,0)==FALSE)  //向服務器發送內容       {         printf("write to pipe failed!/n");         break;       }       else       {         printf("To Server: data = %s, size = %d/n", buf, wlen);         char rbuf[256] = "";         DWORD rlen = 0;         ReadFile(hPipe, rbuf, sizeof(rbuf), &rlen, 0);  //接受服務發送過來的內容         printf("From Server: data = %s, size = %d/n", rbuf, rlen);       }       Sleep(1000);     }     CloseHandle(hPipe);//關閉管道   }   system("pause");   return 0;}以上這篇C/C++ 進程通訊(命名管道)的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選