jamin on February 20th, 2003

In Part I I listed some code to create a server and client for .NET remoting. The client requests an object from the server and then can work with that object. In this example I have some code that works similarly except we take an existing object and publish it for a client to use. This is perfect for what I ultimately want–a way to “remote control” an application such as an instant messenger client using some exposted API.

click below for the code.

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
  {
    private 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();
      // Publish our object
      RemotingServices.Marshal(obj, "SampleObject");
      System.Console.WriteLine("Press the enter key to exit...");
      System.Console.ReadLine();
    }
  }
}

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)
    {
      // Create a channel for communicating w/ the remote object
      // Notice no port is specified on the client
      TcpChannel chan = new TcpChannel();
      ChannelServices.RegisterChannel(chan);

      // Create an instance of the remote object
      SampleObject obj = (SampleObject) Activator.GetObject(
        typeof(TestCode.Remoting.SampleObject),
        "tcp://localhost:8080/SampleObject" );
      // Use the object
      if( obj.Equals(null) )
      {
        System.Console.WriteLine("Error: unable to locate server");
      }
      else
      {
        Console.WriteLine("counter: {0}", obj.GetCount());
      }
    }
  }
}

Tags:

One Response to “.NET Remoting with Mono Part II”

  1. Wondering whether you did anything else with remoting I currently have an application where the client runs -and was compiled- on a windows platform using the .NET Redistributable of microsoft and the server app runs on Linux and has been compiled with mono.
    When I publish my object on the server the client can’t consume it with some type mismatch error. had that before?
    Regards.