This is in three parts:
One common library to implement the remoting object,
One server and one client.
Common library (simple library project):
// SimpleRemoting.cs
using System;
using System.Runtime.Remoting;
// Add a reference to System.Runtime.Remoting
namespace SimpleRemotingLibrary
{
/// <summary>
/// This is a common object for the remoting
/// </summary>
public class RemoteHello : MarshalByRefObject
{
static string msg = “Hello, Universe of .NET”;
public string SayHello()
{
Console.WriteLine(RemoteHello.msg);
return msg;
}
}
}
// HelloServer.cs
using System;
// Add a reference to System.Runtime.Remoting
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
// Add a reference to SimpleRemotingLibrary
using SimpleRemotingLibrary;
public class RemoteServer : MarshalByRefObject
{
[STAThread]
public static void Main(string[] args)
{
TcpChannel channel = new TcpChannel(4000);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteHello),
“HelloDotNet”,
WellKnownObjectMode.Singleton);
System.Console.WriteLine(“Hit <enter> to exit…”);
System.Console.ReadLine();
}
}
// HelloClient.cs
using System;
// Add a reference to System.Runtime.Remoting
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
// Add a reference to SimpleRemotingLibrary
using SimpleRemotingLibrary;
class RemoteClient
{
[STAThread]
static void Main(string[] args)
{
try
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
RemoteHello h = (RemoteHello) Activator.GetObject(typeof(RemoteHello),
“tcp://127.0.0.1:4000/HelloDotNet”);
System.Console.WriteLine( h.SayHello() );
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
System.Console.ReadLine();
}
}
You need to start the server first…