2009-09-10 111 views
0

這是我的Hello World Remoting應用程序。.NET Remoting HelloWorld

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace Remoting__HelloWorld.UI.Client 
{ 
    public interface MyInterface 
    { 
     int FunctionOne(string str); 
    } 
} 

using System; 
using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using System.Runtime.Remoting.Channels.Tcp; 

namespace Remoting__HelloWorld.UI.Client 
{ 
    class MyClient 
    { 
     public static void Main() 
     { 
      TcpChannel tcpChannel = new TcpChannel(); 

      ChannelServices.RegisterChannel(tcpChannel); 

      MyInterface remoteObj = (MyInterface) 
      Activator.GetObject(typeof(MyInterface), "tcp://localhost:8080/FirstRemote"); 

      Console.WriteLine(remoteObj.FunctionOne("Hello World!")); 
     } 
    } 
} 


using System; 
using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using Remoting__HelloWorld.UI.Client; 

namespace Remoting__HelloWorld.UI.Server 
{ 
    public class MyRemoteClass : MarshalByRefObject, MyInterface 
    { 
     public int FunctionOne(string str) 
     { 
      return str.Length; 
     } 
    } 
} 


using System; 
using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using System.Runtime.Remoting.Channels.Tcp; 

namespace Remoting__HelloWorld.UI.Server 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      TcpChannel tcpChannel = new TcpChannel(9999); 

      ChannelServices.RegisterChannel(tcpChannel); 

      RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemoteClass), "FirstRemote", WellKnownObjectMode.SingleCall); 

      System.Console.WriteLine("Press ENTER to quit"); 
      System.Console.ReadLine(); 
     } 
    } 
} 

但運行這個程序後,我收到以下異常:

No connection could be made because the target machine 
actively refused it 127.0.0.1:8080 

我怎樣才能解決這個問題?

回答

2

要麼改變這樣的服務器的客戶端請求:

TcpChannel tcpChannel = new TcpChannel(8080); 

或更改客戶端這樣的:

Activator.GetObject(typeof(MyInterface), "tcp://localhost:9999/FirstRemote"); 

在服務器端,您打開指定端口號的通道(在您的示例中,您使用的是端口9999)。本質上,這告訴服務器在端口9999上'偵聽'傳入的請求。在客戶端,你告訴它要連接的端口號(在你的例子中,你使用的是端口8080)。因此,您有一種情況,即您的服務器在端口9999上進行偵聽,但您的客戶端正嘗試在端口8080上進行連接。這些端口號必須匹配。

3

服務器的TcpChannel是9999對8080

3

你的服務器在端口9999打開通道,而客戶正在尋找8080