windows具名管道

來源:互聯網
上載者:User

標籤:object   可靠   訊號   ==   word   串連   amp   pipe   接受   

具名管道是通過網路來完成進程間的通訊,它屏蔽了底層的網路通訊協定細節。

  將具名管道作為一種網路編程方案時,它實際上建立了一個C/S通訊體系,並在其中可靠的傳輸資料。具名管道伺服器和客戶機的區別在於:伺服器是唯一一個有權建立具名管道的進程,也只有它能接受管道客戶機的串連請求。而客戶機只能同一個現成的具名管道伺服器建立串連。具名管道提供了兩種基本通訊模式,位元組模式和訊息模式。在位元組模式中,資料以一個連續的位元組流的形式在客戶機和伺服器之間流動。而在訊息模式中,客戶機和伺服器則通過一系列不連續的資料單位進行資料的收發,每次在管道上發出一條訊息後,它必須作為一條完整的訊息讀入。

 

#include "stdafx.h"#include <windows.h>#include <stdio.h>int _tmain(int argc, _TCHAR* argv[]){//接受所有安全描述(也就是把管道的串連許可權降到最低).SECURITY_ATTRIBUTES sa;SECURITY_DESCRIPTOR sd; if( InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) ) { // add a NULL disc. ACL to the security descriptor. if (SetSecurityDescriptorDacl(&sd, TRUE, (PACL) NULL, FALSE)) {  sa.nLength = sizeof(sa);  sa.lpSecurityDescriptor =&sd;  sa.bInheritHandle = TRUE;  //建立一個具名管道,在windows中\代表zhuan‘yi兩個\\代表一個\    HANDLE hNamedPipe = CreateNamedPipeA("\\\\.\\pipe\\testName",    PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,    PIPE_TYPE_BYTE, 1, 1024, 1024,0 , &sa);    //檢查是否建立成功    if (hNamedPipe == INVALID_HANDLE_VALUE)    {    printf("create named pipe failed!\n");    }    else  Window  {    printf("create named pipe success!\n");    }    //非同步IO結構    OVERLAPPED op;    ZeroMemory(&op, sizeof(OVERLAPPED));    //建立一個事件核心對象    op.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);    //等待一個用戶端進行串連    BOOL b = ConnectNamedPipe(hNamedPipe, &op);    //當有用戶端進行串連時,事件變成有訊號的狀態    if (WaitForSingleObject(op.hEvent, INFINITE) == 0)    {    printf("client connect success!\n");    }    else    {    printf("client connect failed!\n");    }    //串連成功後,進行通訊,讀寫    char  buff[100];    sprintf_s(buff, 100, "test message from server!");    DWORD cbWrite;    WriteFile(hNamedPipe, buff, strlen(buff), &cbWrite, NULL);    ZeroMemory(buff, 100);    ReadFile(hNamedPipe, buff, 100, &cbWrite, NULL);    //通訊完之後,中斷連線    DisconnectNamedPipe(hNamedPipe);    //關閉管道    CloseHandle(hNamedPipe);   } }    system("pause"); return 0;}

  

 

#include "stdafx.h"#include <windows.h>  #include <stdio.h>  int _tmain(int argc, _TCHAR* argv[]){//檢查具名管道是否存在      BOOL b = WaitNamedPipeA("\\\\.\\pipe\\testName", NMPWAIT_WAIT_FOREVER);      //開啟管道      HANDLE hFile = CreateFileA("\\\\.\\pipe\\testName",          GENERIC_READ | GENERIC_WRITE,          0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);      //檢查是否串連成功      if (!b || hFile == INVALID_HANDLE_VALUE)      {          printf("connect failed!\n");      }      else      {          printf("connect success!\n");      }      //進行通訊      char  buf[100];      ZeroMemory(buf, 100);      DWORD dwRead;      ReadFile(hFile, buf, 100, &dwRead, NULL);      printf(buf);      WriteFile(hFile, "test message for client!", strlen("test message for client!"), &dwRead, NULL);      //關閉管道      CloseHandle(hFile);      system("pause"); return 0;}

  

 

windows具名管道

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.