2015-01-26 3 views
1

안녕하세요 두 프로세스간에 통신하는 데 파이프를 사용하는 데 이상한 오류가 발생했습니다. 즉, 클라이언트 측에서 결코 스트림을 닫지 않는다는 것, 즉 서버의 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"메시지가 표시되지 않는 이유는 무엇입니까? 그리고 내 고객이 스트림을 끝낼 수 있도록 어떻게 고칠 수 있습니까?

+0

당신이 전화 시도 해 봤나하는 데 도움이'sw.Flush(); sw.Close(); '를 명시 적으로? – IllusiveBrian

+0

예. 아무런 차이가없는 것 같습니다. – Noob

+0

'while (! sr.EndOfStream) {...}'해봤습니까? 추신. 클라이언트는 null을 보내지 않고 단순히 연결을 닫고 스트림의 끝이됩니다. – Will

답변

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); 
     } 
+0

게시 한 코드의 의미를 알 수 없습니다. 중요한 차이점은 waitForPipeDrain에 대한 호출이며 도움이된다고 생각하지 않습니다. 어쨌든 통화를 추가했지만 효과가 없습니다. – Noob

+0

그래서 클라이언트 .exe를 테스트 용 서버 .exe와 같은 폴더에 복사했습니다. 또한 프로세스에 첨부하여 프로세스가 하위 프로세스로 실행 중인지 확인할 수도 있습니다.희망이 도움이됩니다. –

+0

도움이 될만한 또 다른 링크는 다음과 같습니다. https://msdn.microsoft.com/enus/library/bb546102%28v=vs.110%29.aspx –

0

당신이 clientServer.DisposeLocalCopyOfClientHandle() 전화를 했습니까? 클라이언트 핸들이 클라이언트에 전달 된 후

DisposeLocalCopyOfClientHandle 방법

from msdn

는 호출해야합니다. 이 메서드가 이 아닌 경우 클라이언트가 PipeStream 개체을 삭제하면 AnonymousPipeServerStream 개체는 알림 을받지 않습니다.

희망이

관련 문제