- // Create two projects (Client, Server)
- // and Add a reference to System.Runtime.Remoting in both.
- // Add the ICalculate Interface in both projects.
- // Client.cs
- using System;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- class Program
- {
- static void Main(string[] args)
- {
- TcpClientChannel channel = new TcpClientChannel();
- ChannelServices.RegisterChannel(channel, false);
- ICalculate Employee = (ICalculate)Activator.GetObject(typeof(ICalculate), "tcp://localhost:5555/Calculate");
- Console.WriteLine(Employee.Sum(5, 5));
- Console.ReadKey();
- }
- }
- public interface ICalculate
- {
- int Sum(int a, int b);
- }
- // Server.cs
- using System;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- class Program
- {
- static void Main(string[] args)
- {
- TcpServerChannel Channel = new TcpServerChannel(5555);
- ChannelServices.RegisterChannel(Channel,false);
- WellKnownServiceTypeEntry remObj = new WellKnownServiceTypeEntry(typeof(Calculate), "Calculate", WellKnownObjectMode.SingleCall);
- RemotingConfiguration.RegisterWellKnownServiceType(remObj);
- Console.WriteLine("Server is running...");
- Console.ReadKey();
- }
- }
- class Calculate : MarshalByRefObject, ICalculate
- {
- public int Sum(int a, int b)
- {
- return a + b;
- }
- }
- interface ICalculate
- {
- int Sum(int a, int b);
- }
C# - Remoting Client Server Example
This code snippet shows how to create a simple remoting client server program. The code shows how to use remoting to invoke a remote object.
No comments:
Post a Comment