在c#中使用命名管道的两个进程之间的双工操作

jam*_*dev 6 c# duplex named-pipes

我试图使用命名管道在同一台机器上的服务器和客户端进程之间进行通信.服务器向客户端发送消息,客户端对其执行某些操作并返回结果,服务器应该得到结果.

这是服务器的代码:

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

class PipeServer
{
    static void Main()
    {
        using (NamedPipeServerStream pipeServer =
            new NamedPipeServerStream("testpipe", PipeDirection.InOut))
        {
            Console.WriteLine("NamedPipeServerStream object created.");

            // Wait for a client to connect
            Console.Write("Waiting for client connection...");
            pipeServer.WaitForConnection();

            Console.WriteLine("Client connected.");
            try
            {
                // Read user input and send that to the client process.
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    Console.Write("Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }

                pipeServer.WaitForPipeDrain();

                using (StreamReader sr = new StreamReader(pipeServer))
                {
                    // Display the read text to the console 
                    string temp;

                    // Wait for result from the client. 
                    while ((temp = sr.ReadLine()) != null)
                    {
                        Console.WriteLine("[CLIENT] Echo: " + temp);
                    }
                }

            }
            // Catch the IOException that is raised if the pipe is 
            // broken or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是客户端的代码:

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

class PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient =
            new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }


            // send the "result" back to the Parent process.
            using (StreamWriter sw = new StreamWriter(pipeClient))
            {
                sw.AutoFlush = true;
                sw.WriteLine("Result");
            }

            pipeClient.WaitForPipeDrain();

        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在服务器代码中,在线pipeServer.WaitForPipeDrain(); 我得到一个ObjectDisposedException,它说"无法访问已关闭的管道".

在将sw.AutoFlush设置为true时,我在客户端代码中也会遇到相同的错误.

基本上我找不到c#中双工命名管道的例子.我要么需要它,要么是一个非经典管道的例子,有两个管道用于读取,一个管道用于父进程和子进程之间的写入.

提前致谢.

TGl*_*zer 7

问题是使用块StreamWriter,它将关闭底层Stream(这是你的管道).如果你不使用那个块它应该工作.

您可以执行以下操作:

using (var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut))
using (var streamReader = new StreamReader(pipeServer))
using (var streamWriter = new StreamWriter(pipeServer))
{
   // ... Your code ..
}
Run Code Online (Sandbox Code Playgroud)

正如约翰内斯埃格尔所指出的那样,StreamWriter冲流Dispose(),所以StreamWriter应该首先处理,因此是最内部的处置对象.