Named pipe Stream include NamedPipeServerStream and NamedPipeClientStream
Named pipe (more flexible)
Allows two-way communication between arbitrary processes on the same computer or different computers across a network.A pipe is good for interprocess communication (IPC) on a single computer: it doesn’t rely on a network transport, which means no network protocol overhead, and it has no issues with firewalls.
Server:
static NamedPipeServerStream serverStream; static int i = 0; static void NamedPipeServerStreamDemo() { serverStream = new NamedPipeServerStream("FredPipeServerStream"); serverStream.WaitForConnection(); System.Timers.Timer timer = new System.Timers.Timer(100); timer.Elapsed += Timer_Elapsed; timer.Start(); } private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { string str = $"i is {i},now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")},Guid is {Guid.NewGuid()}"; byte[] serverBytes = Encoding.UTF8.GetBytes(str); serverStream.Write(serverBytes, 0, serverBytes.Length); i++; }
Client:
static void NamedPipeClientStreamDemo() { var clientStream = new NamedPipeClientStream("FredPipeServerStream"); clientStream.Connect(); while(true) { byte[] receiveData = new byte[100]; int result = clientStream.Read(receiveData, 0, receiveData.Length); string str = Encoding.UTF8.GetString(receiveData); Console.WriteLine($"Client receive {str}"); } }