System.IO.Exception:管道损坏
我有两个通过命名管道相互通信的 .NET 应用程序。 第一次一切都很好,但在发送第一条消息后,服务器将再次监听,WaitForConnection()
方法会抛出 System.IO.Exception
带有消息管道损坏。
为什么我在这里遇到这个异常? 这是我第一次使用管道,但过去我也使用过类似的模式来使用套接字。
代码啊嘿!
服务器:
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);
}
}
客户端:
public void sendDownPipe(string str)
{
using (var pipe = new NamedPipeClientStream(".", "pipename", PipeDirection.Out))
{
using (var stream = new StreamWriter(pipe))
{
stream.Write(str);
}
}
}
第一次调用 sendDownPipe 让服务器很好地打印我发送的消息,但是当它循环回来再次监听时,它就大便了。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将发布我似乎有效的代码 - 我很好奇,因为我从未对管道做过任何事情。 我在相关命名空间中没有找到您为服务器端命名的类,因此这里是基于 NamedPipeServerStream 的代码。 回调的事情只是因为我无法同时处理两个项目。
客户端执行此操作:
我可以在服务器运行的情况下多次重复上述块,没有问题。
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.
And the client does this:
I can repeat above block multiple times with the server running, no prob.
当我在客户端断开连接后从服务器调用 pipeline.WaitForConnection() 时,我遇到了问题。 解决方案是捕获 IOException 并调用 pipeline.Disconnect(),然后再次调用 pipeline.WaitForConnection():
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:
当我将
Environment.Exit(0)
放在 Main 方法的末尾时,我遇到了类似的问题,这显然杀死了整个过程,即使我认为代码无法访问(因为它是在while 循环等待不同的线程停止)。I ran into a similar issue when I put
Environment.Exit(0)
at the end of my Main method, which apparently killed the entire process even though I thought the code was unreachable (because it was after a while loop waiting for a different thread to stop).我遇到了同样的问题 - 这是由使用...结束使用处置服务器的 StreamReader 引起的,这也导致了 NamedPipeServerStream 的删除。 解决方案就是不要使用...结束使用它并信任垃圾收集器。
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.