Skip to content

Instantly share code, notes, and snippets.

@yutopio
Created September 9, 2015 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutopio/c99d25cbc48a1c1c0a19 to your computer and use it in GitHub Desktop.
Save yutopio/c99d25cbc48a1c1c0a19 to your computer and use it in GitHub Desktop.
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.Win32.SafeHandles;
class Program
{
static PipeStream stream;
static StreamWriter writer;
static byte[] readBuffer;
static IAsyncResult asyncResult;
static void Main()
{
Console.WriteLine("Pipe Test");
Console.WriteLine();
Console.WriteLine("Choose the type of pipe to use:");
Console.WriteLine(" 1. Anonymous Pipe");
Console.WriteLine(" 2. Named Pipe");
Console.Write("> ");
var pipeMode = ReadKeySet(new[] { '1', '2' });
Console.WriteLine(pipeMode);
Console.WriteLine();
Console.WriteLine("The role of the program:");
Console.WriteLine(" 1. Server");
Console.WriteLine(" 2. Client");
Console.Write("> ");
var role = ReadKeySet(new[] { '1', '2' });
Console.WriteLine(role);
Console.WriteLine();
if (pipeMode == '1')
if (role == '1')
{
var pipe = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
Console.WriteLine("The handle of the pipe: {0}", pipe.GetClientHandleAsString());
stream = pipe;
}
else
{
Console.Write("Specify the handle of the pipe: ");
var handle = new SafePipeHandle(new IntPtr(int.Parse(Console.ReadLine())), false);
stream = new AnonymousPipeClientStream(PipeDirection.In, handle);
}
else
if (role == '1')
{
Console.Write("Specify the name of the pipe: ");
stream = new NamedPipeServerStream(Console.ReadLine(), PipeDirection.InOut);
}
else
{
Console.Write("Specify the server address (\".\" for local): ");
var server = Console.ReadLine();
Console.Write("Specify the name of the pipe: ");
var name = Console.ReadLine();
stream = new NamedPipeClientStream(server, name, PipeDirection.InOut, PipeOptions.Asynchronous);
}
Console.Write("Wait for the connection... ");
while (!stream.IsConnected) Thread.Sleep(500);
Console.WriteLine("Connected!");
Console.WriteLine("Type \"exit\" to terminate.");
Console.WriteLine();
writer = new StreamWriter(stream);
if (stream.CanRead)
asyncResult = stream.BeginRead(readBuffer = new byte[110], 0, 110, new AsyncCallback(ReadComplete), null);
Input:
Console.Write("> ");
var input = Console.ReadLine().Trim();
if (input.ToLower() == "exit")
{
writer.WriteLine("exit");
goto Exit;
}
if (input.Length > 100) input = input.Substring(0, 100);
writer.WriteLine(input);
goto Input;
Exit:
Exit();
}
static void ReadComplete(IAsyncResult ar)
{
string msg;
stream.EndRead(ar);
Console.WriteLine(msg = Encoding.ASCII.GetString(readBuffer));
if (msg != "exit")
asyncResult = stream.BeginRead(readBuffer = new byte[110], 0, 110, new AsyncCallback(ReadComplete), null);
else
Exit();
}
static void Exit()
{
writer.Close();
stream.EndRead(asyncResult);
writer.Dispose();
stream.Dispose();
Environment.Exit(0);
}
static char ReadKeySet(char[] charSet)
{
Start:
var key = Console.ReadKey(true);
foreach (var ch in charSet)
if (key.KeyChar == ch)
return ch;
goto Start;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment