简体   繁体   English

Windows命名管道无法通过网络工作?

[英]Windows Named pipes not working over network?

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, ie 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 NamedPipeClientStream like shown in the code above then no data is read on the server as 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 "." 唯一的区别是在NamedPipeClientStream在“服务器名称”参数是否 “” or the actual network name (or even localhost). 或实际的网络名称(甚至本地主机)。

Any ideas? 有任何想法吗?

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”和“ 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. TCP445。同样, 默认情况下 ,Windows允许所有传出通信。 You should only need to add an inbound port exception. 您只需要添加入站端口例外。 Your configuration may vary of course. 当然,您的配置可能会有所不同。 .NET 3.5 (C#) Named pipes over network .NET 3.5(C#)通过网络命名的管道

So the conclusion is this: 因此,结论是:

If the Server name parameter in NamedPipeClientStream is something else than "." 如果NamedPipeClientStream中的Server name参数不是“。”。 then the underlying implementation is through a transport layer that supports a maximum of 64k in a single Write operation. 那么底层实现将通过传输层,该传输层在单个Write操作中最多支持64k。 The problem is that if you Write more than 64k then the data just disappears instead of throwing an exception. 问题是,如果您写入的数据超过64k,则数据只会消失而不是引发异常。

The solution is to use the NamedPipeClientStream directly and Write the data in less than 64kb chunks. 解决方案是直接使用NamedPipeClientStream并以少于64kb的块写入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM