2012-02-09 69 views
2

我正試圖製作一個需要通過網絡進行通信的應用程序。我在這裏命名管道的MSDN文檔如下:http://msdn.microsoft.com/en-us/library/bb546085.aspx通過網絡命名管道的C#

我試過從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(); 
    } 
} 
+1

服務器必須能夠接受管道請求! – vulkanino 2012-02-09 16:55:34

+0

@DJ KRAZE - 代碼與上面的MSDN文檔鏈接相同,只是我更改了「。」。如我所說。如何啓用命名管道,或者如何知道服務器是否接受請求? – Michael 2012-02-09 16:58:19

+0

我希望我提供給你的鏈接能夠解決。讓我回答一分鐘以迴應我跟蹤回答了另一個問題。讓我們知道如何解決這個問題,如果你仍然有問題..快樂編碼.. – MethodMan 2012-02-09 17:02:13

回答

3

當您指定計算機名稱,甚至是你自己的計算機的名稱,它使用標準的網絡協議/堆棧的/ etc。

您可能需要打開防火牆端口。 TCP 445.另外,默認爲,Windows允許所有傳出通信。您只需要添加入站端口例外。您的配置可能會有所不同。 .NET 3.5 (C#) Named pipes over network

+0

我啓用了服務器的TCP 445。我發現MSDN文檔中的客戶端代碼存在問題,它總是會關閉控制檯,因爲它需要參數,所以我使用這個代碼:http://www.switchonthecode.com/tutorials/dotnet-35-adds -named-pipes-support - 使用網絡名稱或服務器pc名稱,當它顯示pipeClient.Connect() – Michael 2012-02-09 17:11:32

+0

顯示它僅限於LAN ... http:// stackoverflow。 com/questions/244367/c-sharp-3-5-connecting-named-pipe-across-network - 有沒有1通過路由器在網絡上工作? – Michael 2012-02-09 17:24:38

+0

你想要NamedPipeClientStream或NamedPipeServerStream – MethodMan 2012-02-09 17:35:02