2017-02-06 21 views
1

你好,我在我的NamedPipeServer上有一個arror。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

您的服務器關閉收到第一行後的連接。所以當發送第二行時,管道已經關閉(或「斷開」)。

您應該創建您的服務器側的內部循環,並添加一些命令關閉康恩

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

我試過你的例子Renè,但是客戶端被凍結了。 –