本文介绍了命名管道的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何写一个简单的 - 为它工作需要的最低限度 - 测试应用程序,说明如何使用ICP /命名管道

例如,如何将一个写一个控制台应用程序,其中1写着Hello World的方案2和方案2接收消息和回复罗杰,对程序1。


解决方案

 使用系统;
使用System.IO;
使用System.IO.Pipes;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            startserver的();
            Task.Delay(1000).Wait();
            //客户
            VAR的客户=新NamedPipeClientStream(PipesOfPiece);
            client.Connect();
            StreamReader的读者=新的StreamReader(客户端);
            StreamWriter的作家=新的StreamWriter(客户端);            而(真)
            {
                字符串输入=到Console.ReadLine();
                如果(String.IsNullOrEmpty(输入))破;
                writer.WriteLine(输入);
                writer.Flush();
                Console.WriteLine(reader.ReadLine());
            }
        }        静态无效startserver的()
        {
            Task.Factory.StartNew(()=>
            {
                VAR服务器=新NamedPipeServerStream(PipesOfPiece);
                server.WaitForConnection();
                StreamReader的读者=新的StreamReader(服务器);
                StreamWriter的作家=新的StreamWriter(服务器);
                而(真)
                {
                    变种线= reader.ReadLine();
                    writer.WriteLine(的string.join(,line.Reverse()));
                    writer.Flush();
                }
            });
        }
    }
}

How do I write a simple--bare minimum needed for it to work--test application that illustrates how to use ICP/Named Pipes?

For example, how would one write a console application where Program 1 says "Hello World" to Program 2 and Program 2 receives message and replies "Roger That" to Program 1.

解决方案
using System;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StartServer();
            Task.Delay(1000).Wait();


            //Client
            var client = new NamedPipeClientStream("PipesOfPiece");
            client.Connect();
            StreamReader reader = new StreamReader(client);
            StreamWriter writer = new StreamWriter(client);

            while (true)
            {
                string input = Console.ReadLine();
                if (String.IsNullOrEmpty(input)) break;
                writer.WriteLine(input);
                writer.Flush();
                Console.WriteLine(reader.ReadLine());
            }
        }

        static void StartServer()
        {
            Task.Factory.StartNew(() =>
            {
                var server = new NamedPipeServerStream("PipesOfPiece");
                server.WaitForConnection();
                StreamReader reader = new StreamReader(server);
                StreamWriter writer = new StreamWriter(server);
                while (true)
                {
                    var line = reader.ReadLine();
                    writer.WriteLine(String.Join("", line.Reverse()));
                    writer.Flush();
                }
            });
        }
    }
}

这篇关于命名管道的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 10:28