C/C++ 進程通訊(具名管道)

來源:互聯網
上載者:User

標籤:sage   cti   預設   檔案名稱   程式   成功   disco   server   用戶端串連   

服務端代碼:

// 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("\\\\.\\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("\\\\.\\Pipe\\mypipe"), NMPWAIT_WAIT_FOREVER);    if (!bRet)     {         printf("connect the namedPipe failed!\n");         return 0;     }    HANDLE hPipe = CreateFile(            //管道屬於一種特殊的檔案         TEXT("\\\\.\\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++ 進程通訊(具名管道)

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.