2017-02-06 5 views
1

안녕하세요, 내 NamedPipeServer에 아치가 있습니다.C# NamedPipeServer 새 쓰기 후 깨짐

단일 스트림 WriteLine 을 사용하고 플러시하는 경우 서버와 클라이언트가 정상적으로 작동합니다.

새 줄을 쓰려고하면 오류 IOException 파이프가 끊어졌습니다.

서버 파이프

NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4); 
StreamReader sr = new StreamReader(pipeServer); 
      StreamWriter sw = new StreamWriter(pipeServer); 

      do 
      { 
       try 
       { 
        pipeServer.WaitForConnection(); 
        string test; 
        sw.WriteLine("Waiting"); 
        sw.Flush(); 
        pipeServer.WaitForPipeDrain(); 
        test = sr.ReadLine(); 
        Console.WriteLine(test); 

        if (test.Contains("Mouse")) 
        { 
         Invoke((Action)delegate 
          { 
           listBox1.Items.Add(test); 
           listBox1.SelectedIndex = listBox1.Items.Count - 1; 
          }); 
        } 

        if (test.Contains("Bt1")) 
        { 
         Invoke((Action)delegate 
         { 
          listBox2.Items.Add("BT"); 
         }); 
        } 

       } 

       catch (Exception ex) { throw ex; } 

       finally 
       { 
        pipeServer.WaitForPipeDrain(); 
        if (pipeServer.IsConnected) { pipeServer.Disconnect(); } 
       } 
      } while (true); 

클라이언트 파이프이 문제를 해결하는 방법

 NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", 
"testpipe", PipeDirection.InOut, PipeOptions.None); 
     private void Form1_MouseMove(object sender, MouseEventArgs e) 
     { 


      if (pipeClient.IsConnected != true) { pipeClient.Connect(); } 

      StreamReader sr = new StreamReader(pipeClient); 
      StreamWriter sw = new StreamWriter(pipeClient); 

      string temp; 
      temp = sr.ReadLine(); 

      if (temp == "Waiting") 
      { 
       try 
       { 

        //First Write Working! 
        sw.WriteLine("Mouse Pos: " + e.Location); 
        sw.Flush(); 

        //Second Write i get Exception and Pipe Broken 
        sw.WriteLine("Bt1:1"); 
        sw.Flush(); 

        pipeClient.Close(); 
       } 
       catch (Exception ex) { throw ex; } 
      } 
     } 

?

답변

2

서버 은 첫 번째 줄을 보낸 후 연결을 닫습니다. 그래서 두 번째 라인을 보낼 때, 파이프는 이미 닫혀있다 (또는 "깨진").

당신은 서버 측의 내부 루프를 만들고이 정말 좋은 디자인이 아니라는 CONN에게

NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4); 
StreamReader sr = new StreamReader(pipeServer); 
StreamWriter sw = new StreamWriter(pipeServer); 

do 
{ 
    try 
    { 
     pipeServer.WaitForConnection(); 
     string test; 
     sw.WriteLine("Waiting"); 
     sw.Flush(); 
     pipeServer.WaitForPipeDrain(); 

     // start inner loop 
     while(pipeServer.IsConnected) 
     { 
      test = sr.ReadLine(); 
      Console.WriteLine(test); 

      if (test.Contains("Mouse")) 
      { 
       Invoke((Action)delegate 
       { 
        listBox1.Items.Add(test); 
        listBox1.SelectedIndex = listBox1.Items.Count - 1; 
       }); 
      } 

      if (test.Contains("Bt1")) 
      { 
       Invoke((Action)delegate 
       { 
        listBox2.Items.Add("BT"); 
       }); 
      } 

      // close command 
      if (test == "Close")     
       pipeServer.Disconnect();    
     } 
    } 
    catch (Exception ex) { throw ex; } 
    finally 
    { 
     //If i remove this line, The code Work 
     //pipeServer.WaitForPipeDrain(); 
     //if (pipeServer.IsConnected) { pipeServer.Disconnect(); } 
    } 
} while (true); 

주를 닫 몇 가지 명령을 추가 할 수 있지만 테스트 목적을 위해 할 수 있어야합니다. 연결된 클라이언트의 처리를 자체 메소드 또는 클래스로 캡슐화해야합니다.

catch(Exception ex) { throw ex; }은 다소 유용하지 않습니다 (원래 스택 트레이스를 제거하려면 필요).

+0

예를 들어 보겠습니다. 그러나 클라이언트는 얼어 붙었습니다. –