Introduction
In this sample describes how to build a distribution application on remoting by Tcp protocol.
Background
(Optional) Is there any background to this article that may be useful
such as an introduction to the basic ideas presented?
Using the code
A brief description of how to use the article or code. The
class names, the methods and properties, any tricks or tips.
Blocks of code should be set as style "Formatted"
like this:
Server
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace www.treaple.com
{
public class HelloServer
{
public static void Main(string [] args)
{
TcpServerChannel channel = new TcpServerChannel(8085);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);
System.Console.WriteLine(" to quite...");
System.Console.ReadLine();
}
}
}
Client
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace www.treaple.com
{
public class Client
{
[STAThread]
public static void Main(string [] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
Hello obj = (Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8085/Hi");
if (obj == null) System.Console.WriteLine("Could not find machine!");
else Console.WriteLine(obj.Greeting("John"));
}
}
}
interface:
using System;
namespace www.treaple.com{
public interface IHello {
String HelloMethod(String name);
}
public class HelloServer : MarshalByRefObject, IHello {
public HelloServer() {
Console.WriteLine("HelloServer actived");
}
public String HelloMethod(String name) {
Console.WriteLine("Hello.HelloMethod : {0}", name);
return "hello," + name;
}
}
}
Remember to set the Language of your code snippet using the
Language dropdown.
Use the "var" button to to wrap Variable or class names in
<code> tags like this
.
Points of Interest
Did you learn anything interesting/fun/annoying while writing
the code? Did you do anything particularly clever or wild or zany?
History
If you have any questions about this sample,please visit http://www.treaple.com/bbs/thread-27-1-1.html or send email to me.