命名管道 Named Pipes 通訊學習

2021-06-06 07:31:03 字數 1861 閱讀 6125

學習內容出處:《windows網路程式設計技術》第4章命名管道

命名管道的基本原理:利用微軟網路提供者(msnp)重定向器。

特點:

服務端原始碼,pipeserver.cpp。

//#include "stdafx.h"

#include #include //最多建立例項數

#define num_pipes 1

//管道名稱

#define pipe_name _t("\\\\.\\pipe\\jim")

int _tmain(int argc, _tchar* argv)

; pipehandle = createnamedpipe(pipe_name, pipe_access_duplex,

pipe_type_byte|pipe_readmode_byte, num_pipes, 0, 0, 1000, null);

if (pipehandle == invalid_handle_value)

printf("server is now running\n");

//一直等待,直到客戶端連線

if (connectnamedpipe(pipehandle, null) == 0)

//傳送訊息到客戶端

dword byteswritten;

if (writefile(pipehandle, "server call you", 15, &byteswritten, null) == 0)

//等待客戶端訊息

if (readfile(pipehandle, buffer, sizeof(buffer), &bytesread, null) <= 0)

printf("%.*s\n", bytesread, buffer);

if (disconnectnamedpipe(pipehandle) == 0)

closehandle(pipehandle);

return 0;

}客戶端原始碼,pipeclient.cpp。

//#include "stdafx.h"

#include #include //管道名稱

#define pipe_name _t("\\\\.\\pipe\\jim")

int _tmain(int argc, _tchar* argv)

//create the named pipe file handle

handle pipehandle = createfile(pipe_name, generic_read|generic_write, 0,

(lpsecurity_attributes)null, open_existing,

file_attribute_normal, (handle)null);

if (pipehandle == invalid_handle_value)

//接受服務端訊息

dword bytesread = 0;

tchar buffer[256] = ;

if (readfile(pipehandle, buffer, sizeof(buffer), &bytesread, null) <= 0)

printf("%.*s\n", bytesread, buffer);

//傳送訊息到服務端

dword byteswritten;

if (writefile(pipehandle, "get it, sir!", 12, &byteswritten, null) == 0)

printf("wrote %d bytes\n", byteswritten);

closehandle(pipehandle);

return 0;

}

作業系統實驗(15 16)匿名管道與命名管道通訊

include include include include include intmain printf pipe create success n if fork 0 else close pipe fd 0 close pipe fd 1 return0 分析 父程序通過pipe 呼叫申請建...

命名管道 匿名管道

有名字 則可以通過 名字 開啟相同的管道進行通訊,沒有名字 在核心中沒有明確標識 只能通過 子程序複製父程序的方式實現通訊,複製了檔案描述符 匿名管道只能用於具有親緣關係的程序間通訊。只要在建立程序之前建立管道,後邊的程序都可以實現通訊。命名管道可用於同一主機任意程序間通訊 作業系統在核心提供的一塊...

mysql使用命名管道 命名管道

管道是用於相關過程之間的通訊。我們是否可以使用管道進行不相關的程序通訊,比方說,我們要從乙個終端執行客戶端程式,從另乙個終端執行伺服器程式?答案是否定的。那麼怎樣才能實現不相關的程序通訊,簡單的答案就是使用 命名管道。即使這適用於相關的程序,但是使用命名管道進行相關的程序通訊沒有任何意義。我們使用乙...