System.IO.Exception:管坏了 [英] System.IO.Exception: Pipe is broken
问题描述
我有2个.NET应用程序互相交谈过命名管道。一切都是伟大的第一次通过,但第一个消息之后被发送,服务器将要再听一遍,在waitForConnection方法抛出System.IO.Exception与文本管子坏了。我为什么在这里得到这个例外?这是我第一次用管工作,但类似的模式在过去与插座为我工作。
I have 2 .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 text of "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嗨!
服务器:
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);
}
}
客户端:
public void sendDownPipe(string str)
{
using (var pipe = new NamedPipeClientStream(".", "pipename", PipeDirection.Out))
{
using (var stream = new StreamWriter(pipe))
{
stream.Write(str);
}
}
}
要sendDownPipe第一个呼叫获取服务器打印我送就好了消息,但是当它循环备份再听一遍,它poops。
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.
推荐答案
我会后我的code,似乎工作 - 我很好奇,因为我从来没有与任何管道。我没有找到类命名为服务器端有关的命名空间,所以这里的基础上, NamedPipeServerStream 的code。回调的东西只是因为我不能打扰有两个项目。
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 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();
}
}
和客户做这样的:
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.
这篇关于System.IO.Exception:管坏了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!