में नामित पाइप का उपयोग करके दो प्रक्रियाओं के बीच डुप्लेक्स ऑपरेशन मैं एक ही मशीन पर सर्वर और क्लाइंट प्रक्रिया के बीच संवाद करने के लिए नामित पाइप का उपयोग करने की कोशिश कर रहा हूं। सर्वर क्लाइंट को एक संदेश भेजता है, क्लाइंट इसके साथ कुछ करता है और परिणाम देता है, और सर्वर को परिणाम प्राप्त होता है।सी #
यहां सर्वर के लिए कोड है:
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 के लिए।
अग्रिम में धन्यवाद।
'स्ट्रीमवाइटर' का निपटारा नहीं करता है अंतर्निहित धारा को बंद करता है (यानी, आपका 'पाइप सर्वर')? –
नामित पाइप के शीर्ष पर चल रहे डब्ल्यूसीएफ आपको काम का एक टन बचाएगा। –
कृपया मुझे एक उदाहरण कहां मिल सकता है? – jambodev