2013-02-12 6 views
5

में नामित पाइप का उपयोग करके दो प्रक्रियाओं के बीच डुप्लेक्स ऑपरेशन मैं एक ही मशीन पर सर्वर और क्लाइंट प्रक्रिया के बीच संवाद करने के लिए नामित पाइप का उपयोग करने की कोशिश कर रहा हूं। सर्वर क्लाइंट को एक संदेश भेजता है, क्लाइंट इसके साथ कुछ करता है और परिणाम देता है, और सर्वर को परिणाम प्राप्त होता है।सी #

यहां सर्वर के लिए कोड है:

using System; 
using System.IO; 
using System.IO.Pipes; 

class PipeServer 
{ 
    static void Main() 
    { 
     using (NamedPipeServerStream pipeServer = 
      new NamedPipeServerStream("testpipe", PipeDirection.InOut)) 
     { 
      Console.WriteLine("NamedPipeServerStream object created."); 

      // Wait for a client to connect 
      Console.Write("Waiting for client connection..."); 
      pipeServer.WaitForConnection(); 

      Console.WriteLine("Client connected."); 
      try 
      { 
       // Read user input and send that to the client process. 
       using (StreamWriter sw = new StreamWriter(pipeServer)) 
       { 
        sw.AutoFlush = true; 
        Console.Write("Enter text: "); 
        sw.WriteLine(Console.ReadLine()); 
       } 

       pipeServer.WaitForPipeDrain(); 

       using (StreamReader sr = new StreamReader(pipeServer)) 
       { 
        // Display the read text to the console 
        string temp; 

        // Wait for result from the client. 
        while ((temp = sr.ReadLine()) != null) 
        { 
         Console.WriteLine("[CLIENT] Echo: " + temp); 
        } 
       } 

      } 
      // Catch the IOException that is raised if the pipe is 
      // broken or disconnected. 
      catch (IOException e) 
      { 
       Console.WriteLine("ERROR: {0}", e.Message); 
      } 
     } 
    } 
} 

और यहां ग्राहक के लिए कोड है:, लाइन pipeServer.WaitForPipeDrain पर

using System; 
using System.IO; 
using System.IO.Pipes; 

class PipeClient 
{ 
    static void Main(string[] args) 
    { 
     using (NamedPipeClientStream pipeClient = 
      new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut)) 
     { 

      // Connect to the pipe or wait until the pipe is available. 
      Console.Write("Attempting to connect to pipe..."); 
      pipeClient.Connect(); 

      Console.WriteLine("Connected to pipe."); 
      Console.WriteLine("There are currently {0} pipe server instances open.", 
       pipeClient.NumberOfServerInstances); 
      using (StreamReader sr = new StreamReader(pipeClient)) 
      { 
       // Display the read text to the console 
       string temp; 
       while ((temp = sr.ReadLine()) != null) 
       { 
        Console.WriteLine("Received from server: {0}", temp); 
       } 
      } 


      // send the "result" back to the Parent process. 
      using (StreamWriter sw = new StreamWriter(pipeClient)) 
      { 
       sw.AutoFlush = true; 
       sw.WriteLine("Result"); 
      } 

      pipeClient.WaitForPipeDrain(); 

     } 
     Console.Write("Press Enter to continue..."); 
     Console.ReadLine(); 
    } 
} 

लेकिन सर्वर कोड में(); मुझे ऑब्जेक्ट डिस्प्ले अपवाद मिलता है और यह कहता है "बंद पाइप तक नहीं पहुंच सकता।"

मुझे sw.AutoFlush को सही करने पर क्लाइंट कोड में भी वही त्रुटि मिलती है।

मूल रूप से मुझे C# में डुप्लेक्स नामित पाइप का उदाहरण नहीं मिला। मुझे या तो इसकी जरूरत है, या अनौपचारिक पाइप का एक उदाहरण, दो पाइप पढ़ने के लिए एक और एक माता पिता और एक बच्चे की प्रक्रिया के बीच writting के लिए।

अग्रिम में धन्यवाद।

+1

'स्ट्रीमवाइटर' का निपटारा नहीं करता है अंतर्निहित धारा को बंद करता है (यानी, आपका 'पाइप सर्वर')? –

+0

नामित पाइप के शीर्ष पर चल रहे डब्ल्यूसीएफ आपको काम का एक टन बचाएगा। –

+0

कृपया मुझे एक उदाहरण कहां मिल सकता है? – jambodev

उत्तर

7

समस्या StreamWriter का उपयोग करने वाला ब्लॉक है, जो अंतर्निहित स्ट्रीम को बंद कर देगा (जो यहां आपकी पाइप है)। यदि आप उस ब्लॉक का उपयोग नहीं करते हैं तो इसे काम करना चाहिए।

आप निम्न कर सकता है:

using (var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut)) 
using (var streamReader = new StreamReader(pipeServer)) 
using (var streamWriter = new StreamWriter(pipeServer)) 
{ 
    // ... Your code .. 
} 

के रूप में जोहानिस Egger ने कहा, StreamWriterDispose() पर धारा flushes, तो StreamWriter पहले निपटारा किया जाना चाहिए और इस प्रकार भीतरी सबसे निपटान के लिए वस्तु हो।

+0

धन्यवाद, जिसने समस्या हल की। कोड में एक और समस्या थी, कि रीड्स थोड़ी देर लूप में हो रहे थे और नतीजतन एक डेडलॉक था, थोड़ी देर में बदल गया अगर यह ठीक है और यह ठीक काम करता है। – jambodev

+0

मेरे मामले में यह महत्वपूर्ण था कि लेखक को पाठक के बाद निपटाया जाए क्योंकि लेखक ने निपटारे के दौरान फ्लश किया था, लेकिन जब पाठक का निपटारा किया जाता है तो पहले पाइप का निपटारा किया जाता है और लेखक फ्लश नहीं कर सकता है। –

+0

@ जोहान्सएगर अच्छा बिंदु। – TGlatzer