日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

windows命名管道

發(fā)布時(shí)間:2025/4/16 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 windows命名管道 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

命名管道是通過網(wǎng)絡(luò)來完成進(jìn)程間的通信,它屏蔽了底層的網(wǎng)絡(luò)協(xié)議細(xì)節(jié)。

  將命名管道作為一種網(wǎng)絡(luò)編程方案時(shí),它實(shí)際上建立了一個(gè)C/S通信體系,并在其中可靠的傳輸數(shù)據(jù)。命名管道服務(wù)器和客戶機(jī)的區(qū)別在于:服務(wù)器是唯一一個(gè)有權(quán)創(chuàng)建命名管道的進(jìn)程,也只有它能接受管道客戶機(jī)的連接請求。而客戶機(jī)只能同一個(gè)現(xiàn)成的命名管道服務(wù)器建立連接。命名管道提供了兩種基本通信模式,字節(jié)模式和消息模式。在字節(jié)模式中,數(shù)據(jù)以一個(gè)連續(xù)的字節(jié)流的形式在客戶機(jī)和服務(wù)器之間流動(dòng)。而在消息模式中,客戶機(jī)和服務(wù)器則通過一系列不連續(xù)的數(shù)據(jù)單位進(jìn)行數(shù)據(jù)的收發(fā),每次在管道上發(fā)出一條消息后,它必須作為一條完整的消息讀入。

?

#include "stdafx.h" #include <windows.h> #include <stdio.h>int _tmain(int argc, _TCHAR* argv[]) {//接受所有安全描述(也就是把管道的連接權(quán)限降到最低).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;//創(chuàng)建一個(gè)命名管道,在windows中\(zhòng)代表zhuan'yi兩個(gè)\\代表一個(gè)\ HANDLE hNamedPipe = CreateNamedPipeA("\\\\.\\pipe\\testName", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024,0 , &sa); //檢查是否創(chuàng)建成功 if (hNamedPipe == INVALID_HANDLE_VALUE) { printf("create named pipe failed!\n"); } else Window{ printf("create named pipe success!\n"); } //異步IO結(jié)構(gòu) OVERLAPPED op; ZeroMemory(&op, sizeof(OVERLAPPED)); //創(chuàng)建一個(gè)事件內(nèi)核對象 op.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //等待一個(gè)客戶端進(jìn)行連接 BOOL b = ConnectNamedPipe(hNamedPipe, &op); //當(dāng)有客戶端進(jìn)行連接時(shí),事件變成有信號(hào)的狀態(tài) if (WaitForSingleObject(op.hEvent, INFINITE) == 0) { printf("client connect success!\n"); } else { printf("client connect failed!\n"); } //連接成功后,進(jì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); //關(guān)閉管道 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"); } //進(jì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); //關(guān)閉管道 CloseHandle(hFile); system("pause"); return 0; }

  

?

總結(jié)

以上是生活随笔為你收集整理的windows命名管道的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。