2009-06-30 49 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只是一个测试。