Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created January 2, 2013 23:36
Show Gist options
  • Save gogsbread/4439400 to your computer and use it in GitHub Desktop.
Save gogsbread/4439400 to your computer and use it in GitHub Desktop.
Anonymous pipes server and client examples.
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
namespace dotNetPlayGround
{
class AnonymousPipesServer
{
static void Main()
{
Process adder = new Process();
adder.StartInfo.FileName = "AnonymousPipesClient.exe";
adder.StartInfo.UseShellExecute = false;
adder.StartInfo.RedirectStandardOutput = true;
using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable, 100))
{
adder.StartInfo.Arguments = serverPipe.GetClientHandleAsString();//arguments are passed thru the funnel extended by client.
adder.Start();
serverPipe.DisposeLocalCopyOfClientHandle();
serverPipe.Write(System.Text.Encoding.UTF8.GetBytes("3 4"),0,System.Text.Encoding.UTF8.GetByteCount("3 4"));
}
StreamReader reader = adder.StandardOutput;
Console.WriteLine("Sever:Adder:{0}",reader.ReadToEnd());
adder.WaitForExit();
}
class AnonymousPipesClient
{
static int Main(string[] handle)
{
if (handle.Length > 0)
{
using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, handle[0]))
{
byte[] buffer = new byte[100];
clientPipe.Read(buffer, 0, buffer.Length);
string arguments = Encoding.UTF8.GetString(buffer);
string[] args = arguments.Split(' ');
int a = int.Parse(args[0]);
int b = int.Parse(args[1]);
Console.Write(a + b);
return a + b;
}
}
return -1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment