这里是官方文档,已经很完善了,Server拉Client,Server发Client收。
我根据自己的需要进行了一些修改:
- 要由被拉起的子进程来发送,父进程接收;
- 支持持续发送和接收消息;
- 对上述操作进行类的封装。
代码如下:
Client端代码
using System.IO.Pipes;
namespace TestPipe
{
class AnonymousPipeClient
{
private PipeStream pipeClient;
private StreamWriter sw;
public AnonymousPipeClient(PipeDirection direction, string pipeHandleAsString)
{
try
{
pipeClient = new AnonymousPipeClientStream(direction, pipeHandleAsString);
sw = new StreamWriter(pipeClient);
sw.AutoFlush = true;
Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.", pipeClient.TransmissionMode);
}
catch (Exception e)
{
Console.WriteLine("[CLIENT] Construct error: {0}", e.Message);
}
}
public void Send()
{
try
{
// Send a 'sync message' and wait for client to receive it.
sw.WriteLine("SYNC");
sw.Flush();
pipeClient.WaitForPipeDrain();
// Send the console input to the client process.
Console.Write("[CLIENT] Enter text: ");
sw.WriteLine(Console.ReadLine());
}
catch (IOException e)
{
Console.WriteLine("[CLIENT] Send error: {0}", e.Message);
}
}
}
class Program
{
static void Main(string[] args)
{
AnonymousPipeClient client = new AnonymousPipeClient(PipeDirection.Out, args[0]);
while (true)
{
client.Send();
}
Console.Write("[CLIENT] Press Enter to continue...");
Console.ReadLine();
}
}
}
Server端代码
using System.Diagnostics;
using System.IO.Pipes;
namespace TestPipe
{
class AnonymousPipeServer
{
public AnonymousPipeServerStream PipeServer { get; private set; }
private StreamReader sr;
public AnonymousPipeServer(PipeDirection direction)
{
try
{
PipeServer = new AnonymousPipeServerStream(direction, HandleInheritability.Inheritable);
sr = new StreamReader(PipeServer);
Console.WriteLine("[SERVER] Current TransmissionMode: {0}.", PipeServer.TransmissionMode);
}
catch (Exception e)
{
Console.WriteLine("[SERVER] Construct error: {0}", e.Message);
}
}
public void Receive()
{
while (true)
{
string temp;
bool printed = false;
// Wait for 'sync message' from the server.
do
{
temp = sr.ReadLine();
if (temp == null || temp.Equals(""))
{
if (!printed)
{
Console.WriteLine("[SERVER] Wait for sync...");
printed = true;
}
}
temp = temp != null ? temp : "";
Thread.Sleep(100);
}
while (!temp.StartsWith("SYNC"));
printed = false;
// Read the server data and echo to the console.
temp = sr.ReadLine();
if (temp != null)
{
Console.WriteLine("[SERVER] Echo: " + temp);
}
Thread.Sleep(50);
}
}
}
class Program
{
static void Main()
{
AnonymousPipeServer anonymousPipeServer = new AnonymousPipeServer(PipeDirection.In);
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "../../../../AnonymousPipeClient/bin/Debug/net6.0/AnonymousPipeClient.exe";
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments = anonymousPipeServer.PipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();
anonymousPipeServer.PipeServer.DisposeLocalCopyOfClientHandle();
anonymousPipeServer.Receive();
pipeClient.WaitForExit();
pipeClient.Close();
Console.WriteLine("[SERVER] Client quit. Server terminating.");
}
}
}