Thursday, November 15, 2007

.NET Remoting (3) PassValue




using System;

using System.Collections.Generic;

using System.Text;

namespace RemotingSamples

{

public class HelloServer : MarshalByRefObject

{

public HelloServer()

{

Console.WriteLine("HelloServer activated");

}

public String HelloMethod(String name)

{

Console.WriteLine(

"Server Hello.HelloMethod : {0}", name);

return "Hi there " + name;

}

public String HelloUserMethod(User user)

{

string title;

if (user.Male)

title = "Mr";

else

title = "Ms";

Console.WriteLine(

"Server Hello.HelloMethod : Hello, {0}{1}",

title, user.Name);

return "Hello, " + title + user.Name;

}

}

}



using System;

namespace RemotingSamples

{

[Serializable]

public class User

{

string name = "";

bool male = true;

public User(string name,bool male)

{

this.name = name;

this.male = male;

}

public string Name

{

get{return name;}

set{name = value;}

}

public bool Male

{

get{return male;}

set{male = value;}

}

}

}



using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Remoting.Channels.Http;

using System.IO;

namespace RemotingSamples

{

public class Client

{

public static void Main(string[] args)

{

TcpChannel chan1 = new TcpChannel();

ChannelServices.RegisterChannel(chan1);

HelloServer obj1 = (HelloServer)Activator.GetObject(

typeof(RemotingSamples.HelloServer),

"tcp://localhost:8085/SayHello");

if (obj1 == null)

{

System.Console.WriteLine(

"Could not locate TCP server");

}

HttpChannel chan2 = new HttpChannel();

ChannelServices.RegisterChannel(chan2);

HelloServer obj2 = (HelloServer)Activator.GetObject(

typeof(RemotingSamples.HelloServer),

"http://localhost:8086/SayHello");

if (obj2 == null)

{

System.Console.WriteLine(

"Could not locate HTTP server");

}

Console.WriteLine(

"Client1 TCP HelloUserMethod {0}",

obj1.HelloUserMethod(new User("John",true)));

Console.WriteLine(

"Client2 HTTP HelloUserMethod {0}",

obj1.HelloUserMethod(new User("David", false)));

Console.ReadLine();

}

}

}



using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Remoting.Channels.Http;

namespace RemotingSamples

{

public class Server

{

public static int Main(string [] args)

{

TcpChannel chan1 = new TcpChannel(8085);

HttpChannel chan2 = new HttpChannel(8086);

ChannelServices.RegisterChannel(chan1);

ChannelServices.RegisterChannel(chan2);

RemotingConfiguration.RegisterWellKnownServiceType

(

typeof(HelloServer),

"SayHello",

WellKnownObjectMode.Singleton

);

System.Console.WriteLine("Press Enter key to exit");

System.Console.ReadLine();

return 0;

}

}

}

In this code, it passes the object of the user-defined class 'User' as a parameter of the HelloUserMethod method in the HelloServer class to the server. Note that the 'User' class must be Serializable. The server class keeps unchanged as the last post.

blog comments powered by Disqus