对于简单的 IPC,我选择了 NamedPipes 在进程(本地)之间进行通信。
由于需求的变化,应该有多个服务器实例,这会导致同一管道名上有多个“侦听器”。
但似乎有一个问题。这些侦听器中只有一个会收到消息,其他所有实例都不会收到消息。有某种“广播”消息的方法吗?
我已经看过了这个问题 https://stackoverflow.com/questions/24701410/async-namedpipes-in-case-of-multiple-pipe-server-instances,这基本上是同一个问题,但没有答案。
CODE:
现在我使用的管道服务器非常类似于这个答案 https://stackoverflow.com/questions/4570653/multithreaded-namepipeserver-in-c-sharp/5054851#5054851
我的客户端(在本例中为发件人)代码是:
public static void SendToPipe(string pipeName, string data)
{
using (var p = new NamedPipeClientStream(pipeName))
{
p.Connect();
using (var w = new StreamWriter(p))
{
w.WriteLine(data);
w.Flush();
}
}
}
respectivly:
static void Main(string[] args)
{
SendToPipe("DEFAULT_PIPE_NAME", "Some string to transmit");
}
最后我找到了一个解决方案(不确定它是最佳的 - 但它有效)。它基于使用NamedPipeClientStream.NumberOfServerInstances。
看我的问题线索 https://stackoverflow.com/questions/24701410/async-namedpipes-in-case-of-multiple-pipe-server-instances/25968108#25968108
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)