示例#1
0
        private static void ServerThread(object data)
        {
            // Setup Pipe Server with name "mypipe"
            NamedPipeServerStream myPipeServer = new NamedPipeServerStream("mypipe", PipeDirection.InOut, numThreads);

            int threadId = Thread.CurrentThread.ManagedThreadId;

            // Awaiting for client connection
            myPipeServer.WaitForConnection();

            Console.WriteLine("Connection found on thread[{0}]", threadId);

            try
            {
                // A security token will be available when client has written to the pipe.
                StreamString theStreamString = new StreamString(myPipeServer);

                // Authenticate the identity to connected client
                theStreamString.WriteString("Hello, Friend");
                string theFilename = theStreamString.ReadString();

                // read file while impersonating client
                ReadFileToStream fileReader = new ReadFileToStream(theStreamString, theFilename);

                Console.WriteLine("Reading file: {0} on thread[{1}] as user: {2}.", theFilename, threadId, myPipeServer.GetImpersonationUserName());

                myPipeServer.RunAsClient(fileReader.startRead);
            }
            catch (IOException exception) // IOException catch borken pipe and disconnected (naming issues etc)
            {
                Console.WriteLine("ERROR: {0}", exception.Message);
            } // END CATCH

            myPipeServer.Close();
        } // END ServerThread()
 public ReadFileToStream(StreamString inString, string inFilename)
 {
     streamString = inString;
     filename     = inFilename;
 }