多线程NamePipeServer在C#多线程、NamePipeServer

2023-09-02 01:33:50 作者:Dummer゛浅时光

喜 我想使用 NamedPipeServerStream 这是新的.NET 3.5 namedpipe通信。 我想写多线程管道服务器。在默认情况下它处理或者我应该写code为。我的管道服务器要处理多个请求的时间

Hi I want to use NamedPipeServerStream which is new from .NET 3.5 for namedpipe communication. I want to write multi-threaded pipe server. is it handled by default or I should write code for that. my pipe server should handle multiple request at a time

任何解决方案或code?

any solution or code ?

推荐答案

每个NamedPipeServerStream实例是一个流实现包装的句柄命名管道的一个实例。你可以(而且多线程管道服务器会)有NamedPipeServerStream的多个实例相同的命名管道:每一个包的句柄,命名管道的不同实例,服务于不同的客户端。命名管道实例(甚至对于相同的配管)保持分开由操作系统,所以,没有必要进行任何显式编码,以保持与服务器分开每个客户端的通信。

Each NamedPipeServerStream instance is a Stream implementation wrapping a handle to an instance of a named pipe. You can (and a multithreaded pipe server will) have multiple instances of NamedPipeServerStream for the same named pipe: each one wraps a handle to a different instance of the named pipe, servicing a different client. Named pipe instances (even for the same pipe) are kept separate by the operating system, so there is no need for any explicit coding to keep each client's communication with the server separate.

你做什么需要code明确的线程模型的服务器。最简单的方法,以多线程服务器中的this SO回答,其中包括一个伪code ++模板。更具扩展性的实现中,如果需要支持大量的并发呼叫者,将使用创建一个专门的线程为每个连接的线程池和异步方法来代替。

What you do need to code explicitly is the threading model for the server. The simplest approach to multithreading the server is explained in this SO answer, which includes a pseudo-code template. More scalable implementations, if large numbers of concurrent callers need to be supported, would use thread pooling and the asynchronous methods instead of creating a dedicated thread for each connection.