One of the problems I’ve seen in my ongoing search to understand Remoting is the issue of security when you publish a remotable object over a tcp channel. I spoke with Miguel and got some ideas from him and coded the following little remoting example which now simply publishes a “factory” which exposes a Login() method. If you give the right username and password it returns the SampleObject, otherwise it gives you null. Code below:
SampleClient.cs:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
public class SampleClient
{
public static void Main(string [] args)
{
ChannelServices.RegisterChannel(new TcpChannel());
SampleFactory fac = (SampleFactory) Activator.GetObject(
typeof(TestCode.Remoting.SampleFactory),
"tcp://localhost:8080/SampleFactory" );
// Use the object
if( fac.Equals(null) )
{
Console.WriteLine( "Error: unable to locate server" );
}
else
{
Console.WriteLine( "Server found. We have a factory." );
SampleObject obj = fac.Login( "test", "test" );
if ( obj == null )
{
Console.WriteLine( "Login failed." );
}
else
{
Console.WriteLine( "Login successful." );
Console.WriteLine( "count: {0}", obj.GetCount() );
}
}
}
}
}
SampleFactory.cs:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
public class SampleFactory : MarshalByRefObject
{
private SampleObject obj;
public SampleFactory( SampleObject obj )
{
this.obj = obj;
}
public SampleObject Login( string username, string password )
{
if (username == "test" && password == "test")
{
Console.WriteLine("LOGIN GOOD");
return obj;
}
else
{
Console.WriteLine("LOGIN BAD");
return null;
}
}
// Make object live forever
public override Object InitializeLifetimeService()
{
return null;
}
}
}
SampleObject.cs:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
public class SampleObject : MarshalByRefObject
{
public int counter;
public SampleObject()
{
counter = 0;
}
public int GetCount()
{
counter++;
return counter;
}
// Make object live forever
public override Object InitializeLifetimeService()
{
return null;
}
}
}
SampleServer.cs:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
public class SampleServer
{
public static void Main(string [] args)
{
// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
// Create an instance of our object
SampleObject obj = new SampleObject();
SampleFactory fac = new SampleFactory( obj );
// Publish our object
RemotingServices.Marshal(fac, "SampleFactory");
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
}
}
}
Makefile:
CSC=mcs --debug
default: all
SampleServer.exe: SampleServer.cs SampleObject.dll SampleFactory.dll
$(CSC) /r:System.Runtime.Remoting /r:SampleObject.dll /r:SampleFactory.dll $<
SampleClient.exe: SampleClient.cs SampleObject.dll SampleFactory.dll
$(CSC) /r:System.Runtime.Remoting /r:SampleObject.dll /r:SampleFactory.dll $<
SampleObject.dll: SampleObject.cs
$(CSC) /r:System.Runtime.Remoting /target:library $<
SampleFactory.dll: SampleFactory.cs
$(CSC) /r:System.Runtime.Remoting /r:SampleObject.dll /target:library $<
all: SampleObject.dll SampleServer.exe SampleClient.exe
clean:
rm -f *.dll *.exe
Tags: Mono










March 13th, 2003 at 8:10 am
As Miguel pointed out, to make this sort of thing more “factory” like, you’d want to instantiate the SampleObject only after a valid login, however I’ve been playing with this stuff with the thought in mind of a situation where you already have an existing object, like an instant messenger or irc bot. and you want to control that instance remotely. So use the approach that works for your application.
Mono rocks! I was speaking with Miguel about how productive Mono makes me feel: