using System; using System.IO; using System.IO.Pipes; using System.Text; using System.Threading; namespace NamedPipes { public class Receiver { public readonly static string PipeName = "testpipe"; public Receiver() { } ~Receiver() { } public void Start() { while (true) { var pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.InOut); int threadId = Thread.CurrentThread.ManagedThreadId; // Wait for a client to connect pipeServer.WaitForConnection(); Console.WriteLine($"Client connected on thread[{threadId}]."); try { // Read the request from the client. Once the client has // written to the pipe its security token will be available. StreamString stream = new StreamString(pipeServer); // Verify our identity to the connected client using a // string that the client anticipates. stream.WriteString("I am the one true server!"); string message = stream.ReadString(); Console.WriteLine($"Received: {message}"); // Read in the contents of the file while impersonating the client. //ReadFileToStream fileReader = new ReadFileToStream(ss, filename); // Display the name of the user we are impersonating. //Console.WriteLine($"Reading file: {filename} on thread[{threadId}] as user: {pipeServer.GetImpersonationUserName()}."); //pipeServer.RunAsClient(fileReader.Start); } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Console.WriteLine($"ERROR: {e.Message}"); } finally { pipeServer.Dispose(); } } } } }