C# System.IO.Exception: 管道坏了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/895445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.IO.Exception: Pipe is broken
提问by Jeff Shattock
I have two .NET applications that talk to each other over a named pipe. Everything is great the first time through, but after the first message is sent, and the server is going to listen again, the WaitForConnection()
method throws a System.IO.Exception
with message Pipe is broken.
Why am I getting this exception here? This is my first time working with pipes, but a similar pattern has worked for me in the past with sockets.
我有两个 .NET 应用程序,它们通过命名管道相互通信。第一次通过时一切都很好,但是在发送第一条消息后,服务器将再次侦听,该WaitForConnection()
方法抛出一条System.IO.Exception
消息管道已损坏。
为什么我在这里得到这个异常?这是我第一次使用管道,但过去类似的模式对我有用过套接字。
Code ahoy!
Server:
代码啊!
服务器:
using System.IO.Pipes;
static void main()
{
var pipe = new NamedPipeServerStream("pipename", PipeDirection.In);
while (true)
{
pipe.Listen();
string str = new StreamReader(pipe).ReadToEnd();
Console.Write("{0}", str);
}
}
Client:
客户:
public void sendDownPipe(string str)
{
using (var pipe = new NamedPipeClientStream(".", "pipename", PipeDirection.Out))
{
using (var stream = new StreamWriter(pipe))
{
stream.Write(str);
}
}
}
The first call to sendDownPipe gets the server to print the message I send just fine, but when it loops back up to listen again, it poops.
对 sendDownPipe 的第一次调用让服务器打印我发送的消息就好了,但是当它循环回去再次收听时,它会大便。
采纳答案by flq
I'll post my code that seems to work - I was curious since I never did anything with pipes. I didn't find the class you name for the server-side in the relevant namespace, so here's the code based on the NamedPipeServerStream. The callback stuff is just because I couldn't be bothered with two projects.
我会发布我的代码似乎有效 - 我很好奇,因为我从来没有用管道做过任何事情。我没有在相关命名空间中找到您为服务器端命名的类,所以这里是基于NamedPipeServerStream的代码。回调的东西只是因为我不能被两个项目打扰。
NamedPipeServerStream s = new NamedPipeServerStream("p", PipeDirection.In);
Action<NamedPipeServerStream> a = callBack;
a.BeginInvoke(s, ar => { }, null);
...
private void callBack(NamedPipeServerStream pipe)
{
while (true)
{
pipe.WaitForConnection();
StreamReader sr = new StreamReader(pipe);
Console.WriteLine(sr.ReadToEnd());
pipe.Disconnect();
}
}
And the client does this:
客户端这样做:
using (var pipe = new NamedPipeClientStream(".", "p", PipeDirection.Out))
using (var stream = new StreamWriter(pipe))
{
pipe.Connect();
stream.Write("Hello");
}
I can repeat above block multiple times with the server running, no prob.
我可以在服务器运行的情况下多次重复上述块,没有问题。
回答by John Galt
The problem for me has occurred when I would call pipe.WaitForConnection() from the server, after the client disconnected. The solution is to catch the IOException and call pipe.Disconnect(), and then call pipe.WaitForConnection() again:
在客户端断开连接后,当我从服务器调用 pipe.WaitForConnection() 时,我的问题就出现了。解决方法是捕获IOException并调用pipe.Disconnect(),然后再次调用pipe.WaitForConnection():
while (true)
{
try
{
_pipeServer.WaitForConnection();
break;
}
catch (IOException)
{
_pipeServer.Disconnect();
continue;
}
}
回答by Ond?ej
I had the same problem - it is caused by disposing server's StreamReader by Using...End Using, which also take down NamedPipeServerStream. Solution is simply don't Using...End Using it and trust in garbage collector.
我遇到了同样的问题 - 它是由使用...结束使用处理服务器的 StreamReader 引起的,这也取消了 NamedPipeServerStream。解决方案就是不要使用...结束使用它并信任垃圾收集器。