Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.IO;
  5. using System.IO.Pipes;
  6.  
  7. namespace Receta.CSharp.R0526
  8. {
  9.     public class UsoNamedPipeClientStream
  10.     {
  11.         public static void Main()
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             using (NamedPipeClientStream clientePipe = new NamedPipeClientStream(".", "servidor", PipeDirection.In))
  16.             {
  17.                 Console.WriteLine ("Se ha creado un objeto `NamedPipeClientStream`.");
  18.                
  19.                 // Conexión a named pipe hasta que haya uno disponible:
  20.                 Console.WriteLine ("Intentando conectar a un named pipe...");
  21.                 clientePipe.Connect();
  22.                
  23.                 Console.WriteLine ("Se ha conectado a un servidor named pipe.");
  24.                 Console.WriteLine ("Actualmente existen {0} named pipe en modo servidor abiertos.\n", clientePipe.NumberOfServerInstances.ToString());
  25.                
  26.                 // Lectura de datos y envío de estos al cliente:
  27.                 using (StreamReader sr = new StreamReader(clientePipe))
  28.                 {
  29.                     // Lee el mensaje enviado desde el servidor:
  30.                     string mensajeServidorPipe;
  31.                    
  32.                     while ((mensajeServidorPipe = sr.ReadLine()) != null)
  33.                     {
  34.                         Console.WriteLine ("Mensaje recibido desde el servidor pipe: {0}", mensajeServidorPipe);
  35.                     }
  36.                 }
  37.             }
  38.            
  39.             Console.WriteLine(Environment.NewLine);
  40.         }
  41.     }
  42. }