-
-
Save JenHsuan/7f5d4cad6f871c9c5c609be3978356ce to your computer and use it in GitHub Desktop.
Anonymous pipes server and client examples.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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