Windows 命名管道无法通过网络工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5653310/
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
Windows Named pipes not working over network?
提问by Karlth
I'm encountering a strange problems with Named pipes on Windows in C#.
我在 C# 中的 Windows 上遇到命名管道的奇怪问题。
Client:
客户:
NamedPipeClientStream pipeStream = new NamedPipeClientStream("default-PC","mypipe");
pipeStream.Connect();
BinaryWriter sw = new BinaryWriter(pipeStream);
sw.Write((byte[]) data);
Server:
服务器:
NamedPipeServerStream pipeStream = new NamedPipeServerStream("mypipe");
byte[] dataAll = null;
pipeStream.WaitForConnection();
dataAll = new BinaryReader(pipeStream).ReadBytes(1024 * 1000 * 512);
--
——
If I use "." as a server name in the constructor for the NamedPipeClientStream everything works correctly, i.e. the server fills the dataAll object.
如果我使用“.” 作为 NamedPipeClientStream 构造函数中的服务器名称,一切正常,即服务器填充 dataAll 对象。
Now the strange thing is that if I on the other hand put the network name of the computer("default-PC") in the NamedPipeClientStreamlike shown in the code above then no data is read on the serveras ReadBytes in the server code returns an empty array.
现在奇怪的是,如果我另一方面将计算机的网络名称(“default-PC”)放在 NamedPipeClientStream 中,如上面的代码所示,那么服务器上不会读取数据,因为服务器代码中的 ReadBytes 返回一个空数组。
This could understandable I was running the server and client on two different computers but they are both running on the same machine. The only difference being whether the "server name" parameter in NamedPipeClientStream is"." or the actual network name (or even localhost).
这是可以理解的,我在两台不同的计算机上运行服务器和客户端,但它们都在同一台机器上运行。唯一的区别是 NamedPipeClientStream 中的“服务器名称”参数是否为“。” 或实际的网络名称(甚至是本地主机)。
Any ideas?
有任何想法吗?
回答by Jim
I believe that both "." and "localhost" are considered special names and don't use the normal network connections, but a loopback of some sort.
我相信这两个“。” 和“localhost”被认为是特殊名称,不使用正常的网络连接,而是某种环回。
When you specify a computer name, even your own computer's name, it uses the standard network protocols/stack/etc.
当您指定计算机名称时,即使是您自己的计算机名称,它也会使用标准网络协议/堆栈/等。
You probably need to open a firewall port. TCP 445. Also, by default, Windows allows all outgoing communications. You should only need to add an inbound port exception. Your configuration may vary of course. .NET 3.5 (C#) Named pipes over network
您可能需要打开防火墙端口。TCP 445。此外,默认情况下,Windows 允许所有传出通信。您应该只需要添加一个入站端口例外。当然,您的配置可能会有所不同。 .NET 3.5 (C#) 网络命名管道
回答by Karlth
So the conclusion is this:
所以结论是这样的:
If the Server name parameter in NamedPipeClientStream is something else than "." then the underlying implementation is through a transport layer that supports a maximum of 64k in a single Write operation. The problem is that if you Write more than 64k then the data just disappears instead of throwing an exception.
如果 NamedPipeClientStream 中的服务器名称参数不是“.” 那么底层的实现是通过一个传输层,在单个写操作中最多支持 64k。问题是,如果你写入超过 64k,那么数据就会消失而不是抛出异常。
The solution is to use the NamedPipeClientStream directly and Write the data in less than 64kb chunks.
解决办法是直接使用NamedPipeClientStream,以小于64kb的块写入数据。