views:

43

answers:

2

Server:

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

Client:

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

Class:

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

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

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

Connection OK, 8080 port opened, on client side remoteHost is not null, but remoteHost.Name == ""

Why?

A: 

You need to fix up your class to do the actual code to return the properties as shown, I have added the variable myHostName string type to be used for the properties of 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;
   }
}
tommieb75
throwing connection exception
Evl-ntnt
@Evl-ntnt...will amend this answer...
tommieb75
againremoteHost.Name == ""I think problem in class members marshaling
Evl-ntnt
+2  A: 

You need to marshal your specific server instance (h) into the channel, else a default one will be created.

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

MaLio
insert RemotingServices.Marshal(h, "Server"); after registering wellknown service. Nothing changes
Evl-ntnt
I'm sorry all works correct. Thanx a lot
Evl-ntnt