C# NamedPipeServerStream NamedPipeClientStream

来源:https://www.cnblogs.com/Fred1987/archive/2020/06/29/13209275.html
-Advertisement-
Play Games

The piped stream are bidirectional communcation.Meanwhile they can read and write. Write the NamedPipeServerStream and NamedPipeClientStream in two di ...


The piped stream are bidirectional communcation.Meanwhile they can read and write.

Write the NamedPipeServerStream and NamedPipeClientStream in two different hosts.

Today I created a console application to host the NamedPipeServerStream at first,then add another console application in the same solution to host the NamedPipeClientStream. Right click the solution and click properties and select the Multiple startup projects and then set all of their start action as Start as below.

 

 NamedPipeServerStream part

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp42
{
    class Program
    {
        static void Main(string[] args)
        {
            NamedPipeServerStreamDemo();
            Console.ReadLine();
        } 

static int ServerTicks = 0; static void NamedPipeServerStreamDemo() { using(NamedPipeServerStream serverStream=new NamedPipeServerStream("pipedStream202006291649")) { serverStream.WaitForConnection(); while(serverStream.IsConnected) { string msg = string.Empty; for (int i=0;i<10;i++) { msg += $" {ServerTicks++} server now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")},Guid is {Guid.NewGuid()}"; } byte[] byteArr = Encoding.UTF8.GetBytes(msg); serverStream.Write(byteArr, 0, byteArr.Length); byte[] readBytes = new byte[2000]; serverStream.Read(readBytes, 0, readBytes.Length); Console.WriteLine(Encoding.UTF8.GetString(readBytes).Trim(new char[] { '\0' })); Thread.Sleep(2000); } } } } }

 

NamedPipeClientStream

using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            NamedPipeClientStreamDemo();
            Console.ReadLine();
        }

        static int ClientTicks = 0;
        static void NamedPipeClientStreamDemo()
        {
            using (NamedPipeClientStream clientStream = new NamedPipeClientStream("pipedStream202006291649"))
            {
                clientStream.Connect();
                while (clientStream.IsConnected)
                {
                    byte[] clientBytes = new byte[2000];
                    clientStream.Read(clientBytes, 0, clientBytes.Length);
                    Console.WriteLine(Encoding.UTF8.GetString(clientBytes).Trim(new char[] { '\0' }));
                    string clientSendMsg = $"{ClientTicks++},Client now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}";
                    byte[] sendBytes = Encoding.UTF8.GetBytes(clientSendMsg);
                    clientStream.Write(sendBytes, 0, sendBytes.Length);
                }
            }
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 初次看到原文我是有一些震撼的,原來作為開發人員,閑暇時間還算可以做這麼多有趣程式的開發。閱讀時暫且拋棄你所使用的語言的限制,你是否也能夠在“無聊”之時找到一個開發者的樂趣。 閱讀以下內容時重點關註項目的創意性,並結合自己的獨特經歷進行拓展,你一定也能夠找到編程的樂趣所在。很多項目都可以通過不同的技術 ...
  • 前言 本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。 知識點 • 企業資產介紹 • 財務分析方法 • 企業資產數據爬取 • 企業資產數據展示 企業資產介紹 企業的資產包括流動資產、固定資產、無形資產、股東權益等等,本次給大家介紹 ...
  • 主要梳理一下SpringBoot2.x的依賴關係和依賴的版本管理,依賴版本管理是開發和管理一個SpringBoot項目的前提。 SpringBoot其實是通過starter的形式,對spring-framework進行裝箱,消除了(但是相容和保留)原來的XML配置,目的是更加便捷地集成其他框架,打造 ...
  • 1.迭代器 迭代器為我們提供了統一遍歷容器(List/Map/Set)的方式 1.遍歷List或Set 2.遍歷Map 2.Collections工具類 類java.util.Collections提供對Set、List、Map進行排序、填充、查找元素的輔助方法 1.void sort(List): ...
  • 為了方便大家理解我把之前方案的圖片複製過來了,如下: 上圖的方案存在一個問題,就是我們今天文章要聊的內容。 這個問題就是當 MQ Consumer 收到消息後,就直接發佈 Event 了,如果是同步的,沒有問題。如果某個 EventListener 中處理失敗了,那麼這條消息將不會 ACK。 如果是 ...
  • 帶你捅破窗戶紙 Bash Shell 30min 過家家.zip 備註 : @博客園 : 1. 為什麼不支持 pdf 上傳了呀 2. 網站分類不好用 3. 排版OA工具升級下, 例如 markdown 寫出來好醜. 嘗試升級下呢 ? ...
  • 在項目開發中經常會遇到花錢抽獎類型的需求。但是老闆總是擔心用戶用小錢抽到大獎。這樣會導致項目虧損。下邊這段代碼可以有效制止抽獎項目虧錢。 個人獎池: 語言:thinkphp redis mysql 表:desire抽獎商品表 desire_log用戶抽獎獎品表 user_desire_log用戶抽獎 ...
  • 知識點 關鍵字,常用類(super,static,final): super 子類對父類的引用,只能在非靜態方法中使用 引用父類的成員變數的格式為 super.成員變數名稱 引用父類的非靜態方法的格式為 super.方法名(參數列表) 引用父類的構造方法的格式為 super(參數列表) final ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...