<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Security.Principal;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.IO.Pipes;

namespace PipeConnectionClient
{
    class Program
    {
        private static int numClients = 2;

        public static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (args[0] == "spawnclient")
                {
                    NamedPipeClientStream myPipeClient = new NamedPipeClientStream(".", "mypipe", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);

                    Console.WriteLine("Connecting to the server...");

                    myPipeClient.Connect();

                    StreamString theStreamString = new StreamString(myPipeClient);

                    // A signature string was hard coded in PipeConnection Server Side,
                    // Before we do the action, we need to verify and validate the server
                    if (theStreamString.ReadString() == "Hello, Friend")
                    {
                        // Sending the name of the file which will be returned by the server later
                        theStreamString.WriteString("openthis.txt");

                        Console.Write(theStreamString.ReadString());
                    } // END IF
                    else
                    {
                        Console.WriteLine("Server not found.");
                    } // END ELSE

                    myPipeClient.Close();

                    Thread.Sleep(4000);
                } // END IF
            }
            else
            {
                Console.WriteLine("Name pipe client stream with impersonatin example");
                startClient();
            } // END ELSE
            
        } // END main()

        // This method create pipe process for client
        private static void startClient()
        {
            int count;
            string currentProcessName = Environment.CommandLine;
            Process[] pList = new Process[numClients];

            Console.WriteLine("Spawning client processes...");

            if (currentProcessName.Contains(Environment.CurrentDirectory))
            {
                currentProcessName = currentProcessName.Replace(Environment.CurrentDirectory, String.Empty);
            } // END IF

            currentProcessName = currentProcessName.Replace("\\", String.Empty);
            currentProcessName = currentProcessName.Replace("\"", String.Empty);

            for (count = 0; count < numClients; count++)
            {
                pList[count] = Process.Start(currentProcessName, "spawnclient");
            } // END FOR

            while (count > 0)
            {
                for (int pCount = 0; pCount < numClients; pCount++)
                {
                    if (pList[pCount] != null)
                    {
                        if (pList[pCount].HasExited)
                        {
                            Console.WriteLine("Client process[{0} has exited.]", pList[pCount].Id);
                            pList[pCount] = null;
                            count--;
                        } // END IF
                        else
                        {
                            Thread.Sleep(250);
                        } // END ELSE
                    } // END IF
                } // END FOR
            } // END WHILE

            Console.WriteLine("Client processes finished, exiting.");
            Console.WriteLine("Enter any key to exit the program");
            Console.ReadLine();
        }
    }
}
<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.IO.Pipes;
using System.Threading;

// This is the same class we written in PipeConnection Server side.
// Reuse of codes
namespace PipeConnectionClient
{
    class StreamString
    {
        private Stream ioStream;
        private UnicodeEncoding streamUniEncoding;

        public StreamString()
        {
            // empty
        }

        public StreamString(Stream inIoStream)
        {
            this.ioStream = inIoStream;
            streamUniEncoding = new UnicodeEncoding();
        }

        public string ReadString()
        {
            int length;

            length = ioStream.ReadByte() * 256;
            length += ioStream.ReadByte();

            byte[] inBuffer = new byte[length];
            ioStream.Read(inBuffer, 0, length);

            return streamUniEncoding.GetString(inBuffer);
        } // END ReadString()

        public int WriteString(string inOutputString)
        {
            byte[] outputBuffer = streamUniEncoding.GetBytes(inOutputString);
            int length = outputBuffer.Length;

            if (length > UInt16.MaxValue)
            {
                length = (int)UInt16.MaxValue;
            } // END IF

            ioStream.WriteByte((byte)(length/256));
            ioStream.WriteByte((byte)(length & 255));
            ioStream.Write(outputBuffer, 0, length);
            ioStream.Flush();

            return outputBuffer.Length + 2;
        } // END WriteString()

    }
}
