←Select platform

PipeClient Class

Summary

A class for a client side communication between two processes.

Syntax

C#
C++/CLI
[SerializableAttribute()] 
public class PipeClient : NamedPipeStream<NamedPipeClientStream> 
public: 
   [SerializableAttribute] 
   ref class PipeClient : NamedPipeStream<NamedPipeClientStream^> 

Remarks

The PipeClient class is used for the client side of communication between a client process and server process.

The PipeServer and the PipeClient classes are used together:

PipeClient derives from NamedPipeStream which uses Named Pipes for inter process communication.

Example

C#
using Leadtools.Dicom.AddIn; 
using Leadtools.Dicom.AddIn.Common; 
using Leadtools.Dicom.Common.Extensions; 
 
///  
// This example shows how a client and server can communicate using a named pipe 
public static class PipeServerExample 
{ 
 
   static AutoResetEvent clientFinishedEvent = new AutoResetEvent(false); 
   static AutoResetEvent serverFinishedEvent = new AutoResetEvent(false); 
   private static void Client_ReceivedMessage(object sender, ReceivedMessageEventArgs e) 
   { 
      if (e.Message != null && e.Message.Length > 0) 
      { 
         ServiceMessage serviceMessage = AddInUtils.BinaryDeSerialize<ServiceMessage>(e.Message); 
         string msg = $"Client received message: Service[{serviceMessage.Service}]: {serviceMessage.Message}"; 
         Console.WriteLine(msg); 
      } 
      clientFinishedEvent.Set(); 
   } 
 
   private static void Server_ReceivedMessage(object sender, Leadtools.Dicom.AddIn.Common.ReceivedMessageEventArgs e) 
   { 
      if (e.Message != null && e.Message.Length > 0) 
      { 
         ServiceMessage serviceMessage = AddInUtils.BinaryDeSerialize<ServiceMessage>(e.Message); 
         string msg = $"Server received message: Service[{serviceMessage.Service}]: {serviceMessage.Message}"; 
         Console.WriteLine(msg); 
 
         PipeServer pipeServer = sender as PipeServer; 
         if (pipeServer != null) 
         { 
            ServiceMessage serverServiceMessage = new ServiceMessage(); 
            serverServiceMessage.Service = "MyServiceName"; 
            serverServiceMessage.Message = "MyServerMessage"; 
            Console.WriteLine($"Server sending message: {serverServiceMessage.Message}"); 
            pipeServer.SendMessage(serverServiceMessage); 
         } 
      } 
      serverFinishedEvent.Set(); 
   } 
 
   private static void Server_ReadFromPipeExceptionEvent(object sender, ReadFromPipeExceptionEventArgs e) 
   { 
      Console.WriteLine(e.ReadPipeException.Message); 
   } 
 
   public static void RunClient(string pipeName) 
   { 
      PipeClient client = new PipeClient(pipeName); 
      client.ReceivedMessage += Client_ReceivedMessage; 
      client.Start(); 
      string clientMessage = "MyClientMessage"; 
      Console.WriteLine($"Client sending message: {clientMessage}"); 
      client.SendMessage(clientMessage); 
      clientFinishedEvent.WaitOne(); 
   } 
 
   public static void RunServer(string pipeName) 
   { 
      PipeServer server = new PipeServer(pipeName, 10); 
      server.ReadFromPipeExceptionEvent += Server_ReadFromPipeExceptionEvent; 
      server.ReceivedMessage += Server_ReceivedMessage; 
      server.Start(); 
      serverFinishedEvent.WaitOne(); 
   } 
 
   public static void PipeServerSample() 
   { 
      // For simplicity, this example runs two threads that communicate using a named pipe. 
      // However, the client and server can also communicate when running in different processes. 
      string pipeName = "MyPipeName"; 
 
      Task taskServer = Task.Run(() => RunServer(pipeName)); 
      Task taskClient = Task.Run(() => RunClient(pipeName)); 
 
      Task[] tasks = { taskServer, taskClient }; 
 
      Task.WaitAll(tasks); 
   } 

Requirements

Target Platforms

See Also

PipeClient Members

Leadtools.Dicom.AddIn.Common Namespace

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Dicom.AddIn Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.