40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using System;
|
|
using System.IO.Pipes;
|
|
using System.Security.Principal;
|
|
using System.Threading;
|
|
|
|
|
|
namespace NamedPipes
|
|
{
|
|
public class Sender
|
|
{
|
|
public Sender() { }
|
|
public void SendMessage(string message)
|
|
{
|
|
var pipeClient = new NamedPipeClientStream(".", Receiver.PipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);
|
|
Console.WriteLine("Connecting to server...\n");
|
|
pipeClient.Connect(5000);
|
|
Console.WriteLine("Connected.\n");
|
|
|
|
var ss = new StreamString(pipeClient);
|
|
// Validate the server's signature string.
|
|
if (ss.ReadString() == "I am the one true server!")
|
|
{
|
|
// The client security token is sent with the first write.
|
|
// Send the name of the file whose contents are returned
|
|
// by the server.
|
|
ss.WriteString(message);
|
|
|
|
// Print the file to the screen.
|
|
Console.Write(ss.ReadString());
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Server could not be verified.");
|
|
}
|
|
pipeClient.Dispose();
|
|
// Give the client process some time to display results before exiting.
|
|
Thread.Sleep(100);
|
|
}
|
|
}
|
|
} |