我正在使用命名管道来连接同一台计算机上的服务器和客户端。服务器代码在PowerShell中,而客户端代码在C#中。
服务器代码:
$pipe = new-object System.IO.Pipes.NamedPipeServerStream('testpipe',[System.IO.Pipes.PipeDirection]::InOut)
$pipe.WaitForConnection()
$sw = new-object System.IO.StreamWriter $pipe
$sw.WriteLine('Jane')
$sw.Dispose()
$sr = new-object System.IO.StreamReader $pipe
$val = $sr.ReadLine()
$val
$sr.Dispose()
$pipe.Dispose()
客户代码:
NamedPipeClientStream client = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut, PipeOptions.None);
client.Connect();
StreamReader reader = new StreamReader(client);
string temp = reader.ReadLine();
TBox_Name.Text = name;
reader.Dispose();
StreamWriter writer = new StreamWriter(client);
writer.WriteLine("Hello World");
writer.Dispose();
client.Dispose();
从服务器发送的初始消息传递到客户端时,从客户端到服务器的消息在服务器中的$val = $sr.ReadLine()
处失败。它显示了异常Exception calling "ReadLine" with "0" argument(s): "Cannot access a closed pipe."
我在duplex operation between two processes using named pipes in c#中发现了类似的问题。但是他们在两个代码中都使用C#。