匿名管道没有结束流?

时间:2015-01-26 01:38:16

标签: c# pipe

您好我在使用管道在两个进程之间进行通信时遇到了一个奇怪的错误。简而言之,除了客户端从不关闭流之外,一切都能正常运行,这意味着服务器的streamReader.readLine永远不会返回null,从而导致服务器进程永不终止。我确信这是一个简单的问题,但我努力寻找答案。以下是一些相关代码:

服务器端:

using (StreamReader sr = new StreamReader(clientServer))
                    {
                        // Display the read text to the console
                        string temp;
                        int count = 0;

                        while ((temp = sr.ReadLine()) != null)
                        {
                            if (count == 0)
                            {
                                Console.WriteLine("==========Parent Process found text:like==========");
                            }

                            Console.WriteLine(temp);
                            count++;
                        }
                        Console.WriteLine("out of while loop");

                    }

客户项目:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;

class PipeClient
{
static void Main(string[] args)
{
    try
    {
        if (args.Length < 3)
        {
            Console.WriteLine("Invalid number of commandline arguments");
        }

        else
        {
            List<string> inputList = new List<string>();
            List<string> foundMatchList = new List<string>();

            using (PipeStream pipeClientIn =
                new AnonymousPipeClientStream(PipeDirection.In, args[0]))
            {

                using (StreamReader sr = new StreamReader(pipeClientIn))
                {
                    // Display the read text to the console
                    string temp;
                    int count = 0;
                    while ((temp = sr.ReadLine()) != null)
                    {
                        if (count == 0)
                        {

                            Console.WriteLine("==========Client Process Read Text:==========");
                        }
                        Console.WriteLine(temp);
                        inputList.Add(temp);
                        count++;
                    }
                    foreach (var curtString in inputList)
                    {
                        if (curtString.Contains(args[2]))
                        {
                            foundMatchList.Add(curtString);

                        }
                    }
                }
                //Console.WriteLine("released sr");
            }
            // Console.WriteLine("released pipeClientIn");
            using (PipeStream pipeClientOut =
                new AnonymousPipeClientStream(PipeDirection.Out, args[1]))
            {
                using (StreamWriter sw = new StreamWriter(pipeClientOut))
                {

                    sw.AutoFlush = true;
                    foreach (var match in foundMatchList)
                    {

                        sw.WriteLine(match);

                    }

                }


            }
            //Console.WriteLine("released pipeClientOut");

        }


    }
    catch (Exception e)
    {
        /* if (args.Length == 0)
            Console.WriteLine("no arguments");
        foreach(String s in args)
        {
            Console.Write("{0} ", s);
        }*/
        Console.WriteLine(e.Message);
    }


}
}

我已经过测试,可以确认客户端进程已终止。 我试图手动刷新并关闭Client StreamWriter,但这不起作用。 我的整体问题是:为什么我从来没有看到&#34; while while循环&#34;信息?如何修复我的客户端以便结束流?

2 个答案:

答案 0 :(得分:1)

您是否致电 clientServer.DisposeLocalCopyOfClientHandle()

from msdn

  

应该在之后调用 DisposeLocalCopyOfClientHandle 方法   客户端句柄已传递给客户端。 如果不是这种方法   调用时,AnonymousPipeServerStream对象不会收到通知   当客户端处理其PipeStream对象时。

希望这会有所帮助

答案 1 :(得分:0)

点击此链接:https://msdn.microsoft.com/en-us/library/system.io.pipes.anonymouspipeserverstream(v=vs.110).aspx

  try
        {
            // Read user input and send that to the client process. 
            using (StreamWriter sw = new StreamWriter(pipeServer))
            {
                sw.AutoFlush = true;
                // Send a 'sync message' and wait for client to receive it.
                sw.WriteLine("SYNC");
                pipeServer.WaitForPipeDrain();
                // Send the console input to the client process.
                Console.Write("[SERVER] Enter text: ");
                sw.WriteLine(Console.ReadLine());
            }
        }
        // Catch the IOException that is raised if the pipe is broken 
        // or disconnected. 
        catch (IOException e)
        {
            Console.WriteLine("[SERVER] Error: {0}", e.Message);
        }