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

C# 在c中使用命名管道在两个进程之间进行双工操作#,c#,named-pipes,duplex,C#,Named Pipes,Duplex,我试图使用命名管道在同一台机器上的服务器和客户端进程之间进行通信。服务器向客户机发送一条消息,客户机对其执行操作并返回结果,服务器应该得到结果 以下是服务器的代码: using System; using System.IO; using System.IO.Pipes; class PipeServer { static void Main() { using (NamedPipeServerStream pipeServer = ne

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

以下是服务器的代码:

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);
            }
        }
    }
}
以下是客户端的代码:

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();
    }
}
但是在服务器代码中,在线pipeServer.WaitForPipeDrain();我得到一个ObjectDisposedException,它说“无法访问封闭管道。”

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

基本上,我在c#中找不到双联命名管道的示例。我要么需要它,要么需要一个anonynous管道的例子,它有两个管道,一个用于读取,一个用于在父进程和子进程之间写入


提前感谢。

问题在于
StreamWriter
的using块,它将关闭底层流(此处是您的管道)。如果你不使用那个块,它应该可以工作

您可以执行以下操作:

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

正如Johannes Egger指出的,
StreamWriter
Dispose()
上刷新流,因此应该首先处理
StreamWriter
,因此它是要处理的最内部对象。

不会处理
StreamWriter
也会关闭底层流(即,您的
pipeServer
)?在命名管道上运行WCF将为您节省大量工作。我在哪里可以看到示例?如果其他人对此有问题,请尝试删除所有使用块(即,仅StreamWriter sw=新StreamWriter(pipeClient);)-这对我来说很有效,我认为当我关闭应用程序时,垃圾收集器将处理正确的处理。谢谢,这解决了问题。代码中还有一个问题,读取是在while循环中进行的,因此出现了死锁,将while更改为If,现在可以正常工作。在我的情况下,重要的是在读取器之后释放writer,因为在释放时writer进行了刷新,但是当读卡器首先被释放时,管道也被释放,而写卡器无法冲洗。