안녕하세요 두 프로세스간에 통신하는 데 파이프를 사용하는 데 이상한 오류가 발생했습니다. 즉, 클라이언트 측에서 결코 스트림을 닫지 않는다는 것, 즉 서버의 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를 수동으로 플러시하고 닫으려고 시도했지만 작동하지 않았습니다. 전반적인 질문은 : "out of while loop"메시지가 표시되지 않는 이유는 무엇입니까? 그리고 내 고객이 스트림을 끝낼 수 있도록 어떻게 고칠 수 있습니까?
당신이 전화 시도 해 봤나하는 데 도움이'sw.Flush(); sw.Close(); '를 명시 적으로? – IllusiveBrian
예. 아무런 차이가없는 것 같습니다. – Noob
'while (! sr.EndOfStream) {...}'해봤습니까? 추신. 클라이언트는 null을 보내지 않고 단순히 연결을 닫고 스트림의 끝이됩니다. – Will