コード例 #1
0
        public static void Main(string[] Args)
        {
            if (Args.Length > 0)
            {
                if (Args[0] == "spawnclient")
                {
                    NamedPipeClientStream pipeClient =
                        new NamedPipeClientStream(".", "testpipe",
                                                  PipeDirection.InOut, PipeOptions.None,
                                                  TokenImpersonationLevel.Impersonation);

                    Console.WriteLine("Connecting to server...\n");
                    pipeClient.Connect();

                    StreamString ss = new StreamString(pipeClient);
                    // Validate the server's signature string
                    if (ss.ReadString() == "I am the one true server!")
                    {
                        // The client security token is sent with the first write.
                        // Send the name of the file whose contents are returned
                        // by the server.
                        ss.WriteString("c:\\textfile.txt");

                        // Print the file to the screen.
                        Console.Write(ss.ReadString());
                    }
                    else
                    {
                        Console.WriteLine("Server could not be verified.");
                    }
                    pipeClient.Close();
                    // Give the client process some time to display results before exiting.
                    Thread.Sleep(4000);
                }
            }
            else
            {
                Console.WriteLine("\n*** Named pipe client stream with impersonation example ***\n");
                StartClients();
            }
        }
コード例 #2
0
        public static void Main()
        {
            Console.WriteLine("*** Named Pipes Client ***");
            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("Connecting to server...\n");

            var pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);

            pipeClient.Connect();

            var ss = new StreamString(pipeClient);

            for (var n = 0; n < 10; n++)
            {
                var tagId = RandomTagId();

                ss.WriteString(tagId);
                Console.WriteLine($"Sent tag {tagId}");

                Thread.Sleep(1000);
            }

            // Indicates to the server that the client has no more data to send.
            ss.WriteString(string.Empty);

            pipeClient.Close();

            // Keep the console open for a while.
            Thread.Sleep(2000);
        }