【问题标题】:C# named pipes over a network网络上的 C# 命名管道
【发布时间】:2012-02-10 01:47:09
【问题描述】:

我正在尝试制作一个需要通过网络进行通信的应用。我在这里关注命名管道的 MSDN 文档:http://msdn.microsoft.com/en-us/library/bb546085.aspx

我已经尝试过 MSDN 中的代码,但没有成功。

我看到了那个“。”必须用客户端的网络名称替换,我这样做了。我尝试了网络名称和服务器 PC 名称,但都无法连接到服务器(我的笔记本电脑)。

现在我不知道该怎么做 - 有什么建议吗? (下面的代码让我“找不到网络路径”)

using System;
using System.IO;
using System.IO.Pipes;

class PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient =
            new NamedPipeClientStream("xxx.xxx.x.x", "testpipe", PipeDirection.InOut))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }
        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}

【问题讨论】:

  • 服务器必须能够接受管道请求!
  • @DJ KRAZE - 代码与上面的 MSDN 文档链接相同,只是我更改了“。”如我所说。如何启用命名管道或如何知道服务器是否正在接受请求?
  • 我希望我提供给你的链接能正常工作..对不起,我一分钟内无法回复我被跟踪回答另一个问题..如果你仍然有,请告诉我们这对你有什么影响问题..快乐编码..

标签: c#


【解决方案1】:

当您指定计算机名称时,即使是您自己的计算机名称,它也会使用标准网络协议/堆栈/等。

您可能需要打开防火墙端口。 TCP 445。此外,默认情况下,Windows 允许所有传出通信。您应该只需要添加一个入站端口例外。当然,您的配置可能会有所不同。 .NET 3.5 (C#) Named pipes over network

【讨论】:

  • 我为服务器启用了 TCP 445。我发现 MSDN 文档上的客户端代码存在问题,它总是会关闭控制台,因为它需要参数,所以我改用此代码:switchonthecode.com/tutorials/… - 使用网络名称或服务器 pc 名称,我得到“执行 pipeClient.Connect() 时找不到网络路径”
  • 显然它仅限于 LAN...stackoverflow.com/questions/244367/… - 是否有 1 个通过路由器在网络上工作?
  • 你想要 NamedPipeClientStream 还是 NamedPipeServerStream
  • 看看这个 MSDN: 网站,它也有一些很好的信息和代码,你能不能展示你正在使用的代码,这样我就可以用我的命名管道来测试它了。。@ 987654324@
  • 两个都需要? NamedPipeClientStream 是我无法连接的问题。上面的链接,证明你可以在网络上使用命名管道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多