【问题标题】:Anonymous Pipe not ending stream?匿名管道没有结束流?
【发布时间】:2015-01-26 01:38:15
【问题描述】:

您好,我在使用管道在两个进程之间进行通信时遇到了一个奇怪的错误。简而言之,程序一切正常,除了客户端从不关闭流,这意味着服务器的 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);
    }


}
}

我已经测试并且可以确认客户端进程终止。 我试图手动刷新并关闭客户端 StreamWriter 但这不起作用。 我的总体问题是:为什么我从来没有看到“超出循环”消息?以及如何修复我的客户端以使其结束流?

【问题讨论】:

  • 您是否尝试过显式调用sw.Flush();sw.Close();
  • 是的。好像没什么区别。
  • 你试过while (!sr.EndOfStream) {...} 吗?附言。客户端不发送 null,它只是关闭连接,作为流的结尾。
  • 我直到现在才知道。然而它也没有任何效果。你是对的,我误解了如果流关闭,sr.readline() 将返回 null 的想法。但是现在可以确定流没有被关闭,但是我不知道为什么。感谢您的意见,我将继续研究它
  • @Will: StreamReader.ReadLine() 实际上会在流结束时返回null。客户端需要做的就是关闭流以指示流结束; ReadLine() 将流的结尾转换为 null 返回值。

标签: c# pipe


【解决方案1】:

您是否调用了 clientServer.DisposeLocalCopyOfClientHandle()

from msdn

DisposeLocalCopyOfClientHandle 方法应该在 客户端句柄已传递给客户端。 如果此方法不 调用,AnonymousPipeServerStream 对象将不会收到通知 当客户端释放其 PipeStream 对象时

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多