简体   繁体   English

允许从远程客户端访问命名管道服务器

[英]Allow access to Named Pipe Server from remote clients

I would like to ask you how to set up possible access to my named pipe server from remote clients. 我想问您如何设置从远程客户端到我的命名管道服务器的访问权限。 Until now I thought that NamedPipes can be used just for inter-process communication on same computer, but based on http://msdn.microsoft.com/en-us/library/windows/desktop/aa365150%28v=vs.85%29.aspx it should be possible by setting PIPE_ACCEPT_REMOTE_CLIENTS / PIPE_REJECT_REMOTE_CLIENTS to allow/ not allow to access from remote computers. 到目前为止,我认为NamedPipes只能用于同一台计算机上的进程间通信,但是基于http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa365150%28v=vs.85% 29.aspx应该可以通过将PIPE_ACCEPT_REMOTE_CLIENTS / PIPE_REJECT_REMOTE_CLIENTS设置为允许/不允许从远程计算机访问。 I did not find simple way how to setup this functionality in .NET. 我没有找到简单的方法来在.NET中设置此功能。 I suppose that PipeSecurity could be somehow used for it but I did not find a simple way. 我想可以通过某种方式使用PipeSecurity,但是我没有找到简单的方法。

My current solution allows to access all users to my named pipe on current machine. 我当前的解决方案允许访问所有用户到当前计算机上的我的命名管道。 Can somebody improve my solution to allow access from another machine as well? 有人可以改善我的解决方案以允许也从另一台计算机进行访问吗?

Thanks. 谢谢。

    public NamedPipeServerStream CreateNamedPipeServer(string pipeName)
    {
            const int BufferSize = 65535; 
            var pipeSecurity = new PipeSecurity();
            pipeSecurity.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.ReadWrite, AccessControlType.Allow));
            pipeSecurity.AddAccessRule(new PipeAccessRule("Administrators", PipeAccessRights.ReadWrite, AccessControlType.Allow));

            return new NamedPipeServerStream(pipeName, PipeDirection.InOut, countOfServerInstancesToCreate, PipeTransmissionMode.Message, PipeOptions.Asynchronous,
                                                               BufferSize,
                                                               BufferSize);
    }

Yes, named pipes can be from/to remote computers. 是的,命名管道可以来自/到远程计算机。 See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx which details: 请参阅http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa365590(v=vs.85).aspx ,其中详细说明:

Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network. 命名管道可用于提供同一计算机上的进程之间或整个网络上不同计算机上的进程之间的通信。

But, this option is not directly supported by .NET. 但是,.NET不直接支持此选项。 The NamedPipeServerStream constructors translates the PipeTransmisionMode into the natives dwPipeMode parameter so you don't have direct access to that. NamedPipeServerStream构造函数将PipeTransmisionMode转换为本地dwPipeMode参数,因此您无权直接访问该参数。

Conceivably you could sneak PIPE_REJECT_REMOTE_CLIENTS in there ( PIPE_ACCEPT_REMOTE_CLIENTS is zero, so you don't have to do anything to support that). 可以想象的是,您可以在其中潜行PIPE_REJECT_REMOTE_CLIENTSPIPE_ACCEPT_REMOTE_CLIENTS为零,因此您无需做任何支持)。 You'd have to OR in a new value for the PipeTransmissionMode parameter. 您必须为PipeTransmissionMode参数输入一个新值。 For example: 例如:

var transmissionMode =
    (PipeTransmissionMode)((int)PipeTransmissionMode.Byte | (0x8 >> 1));

Then create a NamedPipeServerStream instance with transmissionMode . 然后创建一个带有transmissionModeNamedPipeServerStream实例。 But, don't expect any support for it :) 但是,不要期望对此有任何支持:)

this 这个

var transmissionMode = (PipeTransmissionMode)((int)PipeTransmissionMode.Byte | (0x8 >> 1)); var transmissionMode =(PipeTransmissionMode)((int)PipeTransmissionMode.Byte |(0x8 >> 1));

shouldn't work since the PipeStream ctr is implemented like this: 由于PipeStream ctr是这样实现的,因此不应该工作:

if (transmissionMode < PipeTransmissionMode.Byte || transmissionMode > PipeTransmissionMode.Message) throw new ArgumentOutOfRangeException("transmissionMode", SR.GetString("ArgumentOutOfRange_TransmissionModeByteOrMsg")); 如果(transmissionMode <PipeTransmissionMode.Byte || TransmissionMode> PipeTransmissionMode.Message)抛出新的ArgumentOutOfRangeException(“ transmissionMode”,SR.GetString(“ ArgumentOutOfRange_TransmissionModeByteOrMsg”));

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

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