Advertisement
Fhernd

UsoNamedPipeServerStream.cs

Jul 26th, 2015
21,711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  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 UsoNamedPipeServerStream
  10.     {
  11.         public static void Main()
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             using (NamedPipeServerStream servidorPipe = new NamedPipeServerStream("servidor", PipeDirection.Out))
  16.             {
  17.                 Console.WriteLine ("Se ha creado un objeto `NamedPipeServerStream`.");
  18.                
  19.                 // Inicia la escucha de conexiones:
  20.                 Console.WriteLine ("A espera de conexiones de clientes...");
  21.                 servidorPipe.WaitForConnection();
  22.                
  23.                 Console.WriteLine ("Se ha conectado un cliente.");
  24.                
  25.                 try
  26.                 {
  27.                     // Lectura de datos y envío de estos al cliente:
  28.                     using (StreamWriter sw = new StreamWriter(servidorPipe))
  29.                     {
  30.                         sw.AutoFlush = true;
  31.                         Console.WriteLine ("Escriba dato a ser enviado al cliente: ");
  32.                         sw.WriteLine(Console.ReadLine());
  33.                     }
  34.                 }
  35.                 // En caso de se haya perdido la conexión con el cliente:
  36.                 catch(IOException e)
  37.                 {
  38.                     Console.WriteLine ("Error: {0}", e.Message);
  39.                 }
  40.             }
  41.            
  42.             Console.WriteLine(Environment.NewLine);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement