2017-02-06 27 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

サーバーは、最初の行を受信した後に接続を閉じます。したがって、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

私はあなたの例を試してみましたが、クライアントはフリーズしています。 –

関連する問題