1. 程式人生 > 其它 >Pipe管道--NamedPipe與AnonymousPipe

Pipe管道--NamedPipe與AnonymousPipe

管道分為命名管道和匿名管道

1.命名管道簡介

"命名管道"或"命名管線"(Named Pipes)是一種簡單的程序間通訊(I P C)機制,Microsoft Windows NT,Windows 2000,Windows 95以及Windows 98均提供了對它的支援(但不包括Windows CE).命名管道可在同一臺計算機的不同程序之間,或在跨越一個網路的不同計算機的不同程序之間,支援可靠的,單向或雙向的資料通訊.用命名管道來設計應用程式實際非常簡單,並不需要事先深入掌握基層網路傳送協議(如T C P / I P或I P X)的知識.這是由於命名管道利用了微軟網路提供者(M S N P)重定向器,通過一個網路,在各程序間建立通訊.這樣一來,應用程式便不必關心網路協議的細節.之所以要用命名管道作為自己的網路通訊方案,一項重要的原因是它們充分利用了Windows NT及Windows 2000內建的安全特性.

2.命名管道作用

這裡有一個可採納命令管道的例子.假定我們要開發一個數據管理系統,只允許一個指定的使用者組進行操作.想像在自己的辦公室中,有一部計算機,其中儲存著公司的祕密.我們要求只有公司的管理人員,才能訪問及處理這些祕密.假定在自己的工作站機器上,公司內的每名員工都可看到網路上的這臺計算機.然而,我們並不希望普通員工取得對機密材料的訪問權.在這種情況下,命名管道可發揮出很好的作用,因為我們可開發一個伺服器應用程式,令其以來自客戶機的請求為準,對公司的祕密進行安全操作.伺服器可將客戶訪問限制在管理人員身上,用Windows NT或新版Windows 2000自帶的安全機制,便可非常輕鬆地做到這一點.在此要記住的一個重點是,將命名管道作為一種網路程式設計方案使用時,它實際上建立一個簡單的客戶機/伺服器資料通訊體系,可在其中可靠地傳輸資料.

3.命名管道優點

使用比較方便,並且不需要宣告埠號之類的,在程式中不需要關心許可權之類的。

4.命名管道限制(我個人認為)

管道只能一對一連結通訊。

5.命名管道通訊的實現以及匿名管道的實現如下:

        #region Anonymouspipe

        static string txID = null;
        static string rxID = null;
        public static void AnonymousPipeServer()
        {
            string clientExe = @"F:\Person\Longteng\LongtengSln\ConsoleAppTestAnonymousPipe\bin\Debug\ConsoleAppTestAnonymousPipe.exe
"; HandleInheritability inherit = HandleInheritability.Inheritable; using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit)) using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit)) { txID = tx.GetClientHandleAsString(); rxID = rx.GetClientHandleAsString(); var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID); startInfo.UseShellExecute = false; // Required for child process Process p = Process.Start(startInfo); tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged rx.DisposeLocalCopyOfClientHandle(); // handle resources. //tx.WriteByte(100); //Console.WriteLine("Server received: " + rx.ReadByte()); //p.WaitForExit(); while (true) { tx.WriteByte(100); Console.WriteLine("Server received: " + rx.ReadByte()); } } } //ClientDemo.exe的內容 public static void AnonymousPipeClient() { //Thread.Sleep(TimeSpan.FromSeconds(1)); //string rxID = args[0]; // Note we're reversing the //string txID = args[1]; // receive and transmit roles. using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID)) using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID)) { //Console.WriteLine("Client received: " + rx.ReadByte()); //tx.WriteByte(200); while (true) { Console.WriteLine("Client received: " + rx.ReadByte()); tx.WriteByte(200); } } } //public static void AnonymousPipeServer1() //{ // string clientExe = @"d:\PipeDemo\ClientDemo.exe"; // HandleInheritability inherit = HandleInheritability.Inheritable; // using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit)) // using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit)) // { // string txID = tx.GetClientHandleAsString(); // string rxID = rx.GetClientHandleAsString(); // var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID); // startInfo.UseShellExecute = false; // Required for child process // Process p = Process.Start(startInfo); // tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged // rx.DisposeLocalCopyOfClientHandle(); // handle resources. // tx.WriteByte(100); // Console.WriteLine("Server received: " + rx.ReadByte()); // p.WaitForExit(); // } //} ////ClientDemo.exe的內容 //public static void AnonymousPipeClient1() //{ // string rxID = args[0]; // Note we're reversing the // string txID = args[1]; // receive and transmit roles. // using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID)) // using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID)) // { // Console.WriteLine("Client received: " + rx.ReadByte()); // tx.WriteByte(200); // } //} #endregion #region Namepipe public static void PipeServer() { var s = new System.IO.Pipes.NamedPipeServerStream("pipedream"); s.WaitForConnection(); while (true) { s.WriteByte(100); Console.WriteLine($"PipeServer 收到 客戶端 資料:{s.ReadByte()}"); } } public static void PipeClient() { var s = new System.IO.Pipes.NamedPipeClientStream("pipedream"); s.Connect(); while (true) { Console.WriteLine($"PipeClient 收到服務端資料:{s.ReadByte()}"); Thread.Sleep(TimeSpan.FromSeconds(2)); s.WriteByte(200); // Send the value 200 back. } } public static void PipeServerMessage() { var s = new System.IO.Pipes.NamedPipeServerStream("pipedream", PipeDirection.InOut, 1, PipeTransmissionMode.Message); s.WaitForConnection(); while (true) { byte[] msg = Encoding.UTF8.GetBytes("Hello"); s.Write(msg, 0, msg.Length); Console.WriteLine($"PipeServer 服務端 資料:{Encoding.UTF8.GetString(ReadMessage(s))}"); } } public static void PipeClientMessage() { //var s = new NamedPipeClientStream("pipedream"); var s = new NamedPipeClientStream("192.168.1.199", "pipedream"); s.Connect(); s.ReadMode = PipeTransmissionMode.Message; while (true) { Console.WriteLine($"PipeClient 收到資料:{Encoding.UTF8.GetString(ReadMessage(s))}"); Thread.Sleep(TimeSpan.FromSeconds(2)); byte[] msg = Encoding.UTF8.GetBytes("Hello right back!1111"); s.Write(msg, 0, msg.Length); Console.WriteLine($"PipeClient 傳送資料:Hello right back!111"); //Thread.Sleep(TimeSpan.FromSeconds(2)); //s.WriteByte(200); // Send the value 200 back. } } static byte[] ReadMessage(PipeStream s) { MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[0x1000]; // Read in 4 KB blocks do { ms.Write(buffer, 0, s.Read(buffer, 0, buffer.Length)); } while (!s.IsMessageComplete); return ms.ToArray(); } #endregion

參考連結:https://www.cnblogs.com/shanranlei/p/3629901.html

龍騰一族至尊龍騎