Link to home
Start Free TrialLog in
Avatar of Oli999
Oli999

asked on

.net remoting c# example

Hi Experts,

I've been struggling with c# .net remoting examples but i'm not having much look.  i've tried this one un-successfully:

http://www.developer.com/net/cplus/article.php/1479761

Does anyone have any examples of ones that they know work 100%

Cheers
Avatar of rinksno1
rinksno1

//Code for the remotable type
using System;

namespace Bhupinder.Remoting.MarshObject
{
    public class Greatings : MarshalByRefObject
    {
        public Greatings()
        {
            Console.WriteLine("Constructor called");

        }
        ~Greatings()
        {
            Console.WriteLine("Deconstructir called");
        }

        public void PrintText(string txt)
        {
            Console.WriteLine("Hey you need to print : {0}",txt);
        }
    }

}
//*************************
//Code for the Server
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;


namespace Bhupinder.Remoting.Server
{
    class ServerClass
    {
        static void Main(string[] args)
        {
            HttpChannel httpchn = new HttpChannel(8080);
            ChannelServices.RegisterChannel(httpchn,false);
            RemotingConfiguration.RegisterWellKnownServiceType
                (typeof(Bhupinder.Remoting.MarshObject.Greatings), "Greatings",
                WellKnownObjectMode.Singleton);
            Console.WriteLine("Press any key to end the server....");            
            Console.ReadLine();            

        }
    }
}
//*******************************************
//Code for the Client

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using Bhupinder.Remoting.MarshObject;

namespace Bhupinder.Remoting.RemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpChannel httpchn = new HttpChannel();
            ChannelServices.RegisterChannel(httpchn, false);
            Greatings grt = (Greatings)Activator.GetObject(typeof(Greatings), "http://localhost:8080/Greatings");

            if (grt == null)
            {
                Console.WriteLine("Server down please try again after sometime");
                Console.ReadLine();
            }

            else
            {
                grt.PrintText("This will be printed...");
                Console.ReadLine();
            }

        }
    }
}


//***********************

The three sections above must be saved in three different files/projects..
A reference of System.Runtime.Remoting should be added to Server and the client.
This is a working version i have just checked before posting this...


rinks
ASKER CERTIFIED SOLUTION
Avatar of rinksno1
rinksno1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial