Beispiel #1
0
        public static void Main()
        {
            Console.WriteLine("*** Named Pipe Server ***");
            Console.WriteLine("Enter pipe name and press ENTER");

            var pipeName = Console.ReadLine();

            if (string.IsNullOrEmpty(pipeName))
            {
                pipeName = "ChipID";
                Console.WriteLine($"Using default pipe name of \"{pipeName}\"");
            }

            Console.WriteLine("Waiting for client to connect...");

            // Server can only receive input with this pipe, it's not duplex.
            var pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.In, 1);

            // Wait for a client to connect
            pipeServer.WaitForConnection();

            Console.WriteLine("Client connected\n");
            try
            {
                // For reading data from the client.
                var ss = new StreamString(pipeServer);

                while (true)
                {
                    var tagId = ss.ReadString();

                    // Empty string received from client is the trigger to close the connection.
                    if (string.IsNullOrEmpty(tagId))
                    {
                        break;
                    }

                    Console.WriteLine($"Received tag {tagId}");
                }
            }
            catch (IOException e)
            {
                // Catch the exception that is raised if the pipe is broken or disconnected.
                Console.WriteLine($"ERROR: {e.Message}");
            }

            pipeServer.Close();

            // Keep the console open for a while.
            Thread.Sleep(2000);
        }
Beispiel #2
0
        private static void ServerThread(object data)
        {
            NamedPipeServerStream pipeServer =
                new NamedPipeServerStream("testpipe", PipeDirection.InOut, numThreads);

            int threadId = Thread.CurrentThread.ManagedThreadId;

            // Wait for a client to connect
            pipeServer.WaitForConnection();

            Console.WriteLine("Client connected on thread[{0}].", threadId);
            try
            {
                // Read the request from the client. Once the client has
                // written to the pipe its security token will be available.
                StreamString ss = new StreamString(pipeServer);

                // Verify our identity to the connected client using a
                // string that the client anticipates.
                ss.WriteString("I am the one true server!");
                string filename = ss.ReadString();

                // Read in the contents of the file while impersonating the client.
                ReadFileToStream fileReader = new ReadFileToStream(ss, filename);

                // Display the name of the user we are impersonating.
                Console.WriteLine("Reading file: {0} on thread[{1}] as user: {2}.",
                                  filename, threadId, pipeServer.GetImpersonationUserName());
                pipeServer.RunAsClient(fileReader.Start);
            }
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
            pipeServer.Close();
        }
Beispiel #3
0
 public ReadFileToStream(StreamString str, string filename)
 {
     fn = filename;
     ss = str;
 }