只有一个客户端可以连接到命名管道
我目前正在使用 ASP.NET 3.5 和 C# 学习 Windows 中的命名管道。 我编写了一个小型服务器程序,它创建一个命名管道:
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream(pipeName))
{
pipeStream.WaitForConnection();
// do sth.
}
以及一个打开该管道的客户端应用程序,如下所示:
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(pipeName))
{
pipeStream.Connect();
// do sth.
}
只要只有一个客户端连接到该管道,这种方法就可以很好地工作。它既可以读也可以写。如果我尝试连接第二个客户端,代码永远不会超出
pipeStream.Connect();
该行服务器和所有客户端都在同一台计算机上运行。 有什么想法吗?
预先非常感谢您!
I am currently learning about named pipes in Windows using ASP.NET 3.5 and C#.
I wrote a small server program which creates a named pipe:
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream(pipeName))
{
pipeStream.WaitForConnection();
// do sth.
}
and a client application opening the pipe like this:
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(pipeName))
{
pipeStream.Connect();
// do sth.
}
This works great as long as only one client connects to the pipe. It can both read and write. If I try to connect a second client, the code never exceeds the line
pipeStream.Connect();
Both the server and all clients are running on the same machine.
Any ideas?
Thank you very much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在这里查找一些信息:
可以连接到命名管道的客户端数量
并在 MSDN 中: http://msdn .microsoft.com/en-us/library/aa365594%28VS.85%29.aspx
据我了解,您应该创建一个多线程应用程序。主线程应该负责未来的连接,并且每个新连接都应该在新线程中启动。
you can look up some of the info here:
Number of Clients that can connect to a Named Pipe
and here in MSDN: http://msdn.microsoft.com/en-us/library/aa365594%28VS.85%29.aspx
from what i understand, you should create a multi-threaded application. main thread should be responsible for the future connections, and each new connection should be launched in new thread.
感谢您的快速帮助。
我已经在单独的线程中处理了实际的处理,但忘记提及这一点。
不过,一位同事发现了问题:
我有一个 StreamReader,它被服务器端发布的 NamedPipeServerStream-using 块中的另一个 using 块包围。
当该块完成关闭 StreamReader 时,它还由于某种原因断开了 NamedPipeServerStream 的连接。另外,我没有将 pipelineStream.WaitForConnection() 包含在循环中。
Thanks for the fast help.
I already handled the real processing in separate threads but forgot to mention that.
A co-worker found the problem though:
I had a StreamReader enclosed by another using block in the posted NamedPipeServerStream-using block on the server side.
When this block finished closing the StreamReader, it also disconnected the NamedPipeServerStream for some reason. Also I wasn't enclosing the pipeStream.WaitForConnection() in a loop.
一根真正的管道(来自五金店的那个)有两个末端。因此,我希望命名管道也有两个端点,一个用于服务器进程,一个用于客户端进程。
然而,无论最终答案是什么,我建议看一下 WCF - 它支持网络管道绑定(以及许多其他功能,例如二进制 TCP、带有或不带有 WS-* 规范的 SOAP,只需更改配置即可)并处理为您提供服务器激活和多线程。 .net 3.5 已经可用,所以我肯定会选择它。
A real pipe (that one from the hardwre store) has two endings. So I'd expect that the named pipe also has exactly two endpoints, one for the server process and one for the client process.
However, no matter what's the final answer, I'd suggest to have a look at WCF - it supports net pipe binding (and lots of others like binary TCP, SOAP with or without WS-* specs just by changing the config) and handles the server activation and multi threading for you. .net 3.5 is available, so I'd definitely opt for it.
请参阅我的答案,了解如何在.NET中实现多线程管道服务器。
Please see my answer here for how to implement a multi threaded pipe server in .NET.