2010-05-11 70 views
1

服务器:属性的远程处理

Host h = new Host(); 
h.Name = "JARR!!"; 
TcpChannel channel = new TcpChannel(8080); 
ChannelServices.RegisterChannel(channel); 
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Host), "Server", 
       WellKnownObjectMode.Singleton); 

客户:

TcpChannel chan = new TcpChannel(); 
      ChannelServices.RegisterChannel(chan); 
      remoteHost = (Host)Activator.GetObject(typeof(Host), 
"tcp://127.0.0.1:8080/Server"); 

类:

[Serializable] 
    public class Host: MarshalByRefObject 
    { 
     public string Name{get; set;} 
     public Host(){} 

     public Host(string n) 
     { 
      Name = n; 
     } 

     public override string ToString() 
     { 
      return Name; 
     } 
    } 

连接OK,8080口岸开通后,客户端侧远程主机不为空,但REMOTEHOST .Name ==“”

为什么?

回答

2

您需要编组的属性变量myHostName字符串类型您的特定服务器实例(h)插入到通道中,否则会创建一个默认的服务器实例。

System.Runtime.Remoting.RemotingServices.Marshal(...);

+0

插入RemotingServices.Marshal(H, “服务器” );注册知名服务后。没有任何变化 – 2010-05-11 13:06:52

+0

对不起,所有作品都是正确的。非常感谢 – 2010-05-11 13:09:16

-1

你需要修复你的课上做实际的代码返回属性如图所示,我已经加入到被用于Name

 
[Serializable] 
public class Host: MarshalByRefObject 
{ 
    private string myHostName; 

    public string Name{ 
     get{ return this.myHostName; } 
     set{ this.myHostName = value; } 
    } 

    public Host(string n) 
    { 
     this.myHostName = n; 
    } 
    public override string ToString() 
    {   
     return this.myHostName; 
    } 
} 
+0

抛出连接异常 – 2010-05-11 11:15:11

+0

@的Evl-ntnt ...将修改这个答案... – t0mm13b 2010-05-11 12:01:45

+0

再次 remoteHost.Name == “” 我想在类成员问题编组 – 2010-05-11 12:20:21