【问题标题】:Duplex named pipes - maximum number of clients on a single pipe双工命名管道 - 单个管道上的最大客户端数
【发布时间】:2013-10-08 15:31:30
【问题描述】:

使用此答案中的代码 - Async two-way communication with Windows Named Pipes (.Net) - 我发现任何时候连接/客户端的最大数量为 10。

在下面的粗略示例中(这使用多个线程 - 如果使用多个进程会发生同样的事情)客户端 1 到 10 将正常启动和运行。但是客户端 11 和 12 会在调用“ProcessData”时阻塞,最终抛出 TimeoutException。

    public static void Start()
    {
        // Start Server
        new Thread(new ThreadStart(Server.MainRun)).Start();

        // Start Clients
        for (int i = 1; i <= 12; i++)
        {
            Thread.Sleep(500);
            Client c = new Client(i.ToString());
            new Thread(new ThreadStart(c.Run)).Start();
        }
    }

    // Create a contract that can be used as a callback
    public interface IMyCallbackService
    {
        [OperationContract(IsOneWay = true)]
        void NotifyClient();
    }

    // Define your service contract and specify the callback contract
    [ServiceContract(CallbackContract = typeof(IMyCallbackService))]
    public interface ISimpleService
    {
        [OperationContract]
        string ProcessData();
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class SimpleService : ISimpleService
    {
        public string ProcessData()
        {
            // Get a handle to the call back channel
            var callback = OperationContext.Current.GetCallbackChannel<IMyCallbackService>();

            callback.NotifyClient();
            return DateTime.Now.ToString();
        }
    }

    class Server
    {
        public static void MainRun()
        {
            // Create a service host with an named pipe endpoint
            using (var host = new ServiceHost(typeof(SimpleService), new Uri("net.pipe://localhost")))
            {
                host.AddServiceEndpoint(typeof(ISimpleService), new NetNamedPipeBinding(), "SimpleService");
                host.Open();

                Console.WriteLine("Simple Service Running...");
                Console.ReadLine();

                host.Close();
            }
        }
    }

    class Client : IMyCallbackService
    {
        string _id;

        public Client(string ID)
        {
            _id = ID;
        }

        public void Run()
        {
            Console.WriteLine("Starting client : " + _id);
            // Consume the service
            var factory = new DuplexChannelFactory<ISimpleService>(new InstanceContext(this), new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/SimpleService"));
            var proxy = factory.CreateChannel();

            Console.WriteLine(proxy.ProcessData());

            Console.WriteLine("Client finished : " + _id);
        }

        public void NotifyClient()
        {
            Console.WriteLine("Notification from Server");
        }
    }

如果客户端在完成后关闭通道(factory.Close()),那么所有客户端都可以运行。

我了解这个问题 - Number of Clients that can connect to a Named Pipe - 非常相似,但建议没有下限。

这表明 Windows XP 和 2000 机器上的限制为 10 - http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeclientstream.aspx - 除非这发生在 Windows 8 机器和 Windows 2008 服务器上。

有没有办法改变这个限制?我错过了什么明显的东西吗?

【问题讨论】:

    标签: c# .net named-pipes duplex


    【解决方案1】:

    在提出这个问题一年后,Google 把我带到了这里。我想我也可以发帖来帮助任何最终来到这里的人。我认为我知道为什么限制为 10。

    您知道NetNamedPipeBinding.MaxConnections 属性吗?

    获取或设置允许连接到使用命名管道绑定配置的端点的最大连接数,包括入站和出站。 ...此绑定允许的最大命名管道连接数。 默认值为 10。

    【讨论】:

      【解决方案2】:

      “guest”是正确的,来自 MSDN 的 an old blog 帖子证实了这一点仍然适用于当前的 .net 版本。

      它还表明默认设置是为开发环境和“小型”部署定义的。

      从其他设置(例如缓冲区大小)来看,我建议每个连接开销 >8kb。

      我还没有找到任何信息来说明如果该值针对较大的值(例如 >1000)进行调整可能会出现什么问题:API 似乎针对更短、更突发的请求进行了调整,我怀疑对于较大的值它可能只是效率低下(不是内存,而是内部实现) -

      我欢迎在性能/问题(或成功)方面提供证据,大量客户端连接到端点。

      【讨论】:

        猜你喜欢
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2010-12-03
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多