Tuesday, April 15, 2014

Client Server Applicatin in C# Sample Code



.Net Remoting in C#




using System.Collections.Generic;
using System.Linq;
using System.Text;
using lab7_clas;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace clientside
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel());
            Class1 obj = (Class1)Activator.GetObject(typeof(Class1), "tcp://localhost:3128/HelloWorld");
            int count = 0;
            for (count = 0; count < 5; count++)
            {
                obj.Helloworld();
            }
        }
    }
}

Client







Server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using lab7_clas;
namespace lab7_1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpServerChannel channel = new TcpServerChannel(3128);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "HelloWorld", WellKnownObjectMode.SingleCall);
            Console.WriteLine("Press Enter to Stop Server");
            Console.ReadLine();
        }
    }
}




Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab7_clas
{
    public class Class1 : System.MarshalByRefObject
    {
        public Class1()
        {
            Console.WriteLine("This is the Constructor of Myclass");
        }
        public void Helloworld()
        {
            Console.WriteLine("A big Hello to the world");
        }
    }
}

No comments:

Post a Comment