2009-06-30 96 views
2

我使用.Net Remoting並嘗試訪問服務器中修改的遠程對象,這裏是對象def: 這個想法是,我在服務器上創建的MRB對象返回給客戶端,並且在客戶端上調用getString()時,將打印出「Set On Server」。問題與.Net Remoting(C#)

我現在得到的是客戶端上的一個空字符串,所以MRB對象在客戶端調用新的時候並沒有發送給客戶端。 對不起所有的課程等,但它是唯一的方法,我儘可能修剪。

我真正想要的是「設置在服務器上」運行時打印在客戶端上。

using System; 
using System.Runtime.Remoting.Lifetime; 
namespace RemoteType 
{ 
    public class MyRemoteObject : System.MarshalByRefObject 
    { 
     private string sharedString; 
     public string getString() 
     { 
      return sharedString; 
     } 
     public void setString(string value) 
     { 
     sharedString = value; 
     } 
     public MyRemoteObject() 
     { 
      Console.WriteLine("MyRemoteObject Constructor Called"); 
     } 

     public override object InitializeLifetimeService() 
     { 
      return null; 
     } 
     public string Hello() 
     { 
      return "Hello, Welcome to .Net Remoting !"; 
     } 
    } 

知道這裏是服務器:

using System; 
using System.Runtime.Remoting; 
using RemoteType; 
namespace SimpleServer 
{ 
    class SimpleServer 
    { 
     public static MyRemoteObject MRB = null; 
     static void Main(string[] args) 
     { 
      RemotingConfiguration.Configure("RemotingAppServer.exe.config"); 
      MRB = new MyRemoteObject(); 
      MRB.setString("Set on the server"); 
      Console.WriteLine(MRB.getString()); 
      RemotingServices.Marshal((MRB), "MyRemoteObject"); 
      Console.WriteLine("Press return to exit"); 
      Console.ReadLine(); 
     } 
    } 

和.NET遠程配置App.Config中:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.runtime.remoting> 
     <application> 
      <channels> 
       <channel ref="tcp" port="8000" /> 
      </channels> 
      <service> 
       <wellknown mode="Singleton" 
       type="RemoteType.MyRemoteObject, RemoteType" 
       objectUri="MyRemoteObject" /> 
      </service> 
     </application> 
    </system.runtime.remoting> 
</configuration> 

最後客戶端:

using System; 
using System.Runtime.Remoting; 
using RemoteType; 
namespace SimpleClient 
{ 
    class SimpleClient 
    { 
     static void Main(string[] args) 
     { 
      RemotingConfiguration.Configure("RemoteClient.exe.config"); 
      MyRemoteObject robj = new MyRemoteObject(); 
      Console.WriteLine(robj.Hello() + " " + robj.getString()); 
      Console.ReadLine(); 
     } 
    } 
} 

而其配置太:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.runtime.remoting> 
     <application name = "SimpleClient"> 
      <client> 
       <wellknown 
       type="RemoteType.MyRemoteObject,RemoteType" 
       url="tcp://localhost:8000/MyRemoteObject"/> 
      </client> 
      <channels> 
       <channel ref="tcp" port="0"/> 
      </channels> 
     </application> 
    </system.runtime.remoting> 
</configuration> 

回答

2

我測試了你的代碼並且工作完美。我確實設置了三個項目,一個是服務器,另一個是客戶端,第三個項目是服務器和客戶端之間共享的遠程對象。 在遠程app.config中,您可以刪除已知條目,因爲您已經通過代碼對其進行了編組。

+0

當客戶端運行時,您是否看到在客戶端上打印的「在服務器上設置」? – daniel 2009-06-30 22:15:25

0

你確定你的套接字在應用程序結束後立即關閉嗎?

如果測試多次迅速,這可能會導致TCP通信問題

0

難道一個防火牆在您的環境中阻止TCP端口8000?您可以嘗試切換到http只是一個測試。