How to use .Net Remoting in C#

How to use .net Remoting in c#

 

There are three part of Remoting

 

1.RemoteObject (Class Lib)

2.Server App (Console App), and

3.Client App (Console App),

 

 

 

1.RemoteObject

To create RemoteObject.dll Take .NET IDE New Project->YourName.RemoteOject

(for example, Remoting.RemoteObject(Type Class Library). 

By Default.,Class1.cs will come. Change that file name RemoteObject.cs.

 

using System;

 

namespace Remoting.RemoteObject

{

      /// <summary>

      /// Summary description for Class1.

      /// </summary>

      public class RemoteObject : MarshalByRefObject

      {

            public RemoteObject()

            {

                  Console.WriteLine("Remote Object Activated");

            }

            public string ReplyMessage(string msg)

            {

                  Console.WriteLine("Client : "+msg);                             return "Server : Yeah! I’m here";

            }

      }

}

 

Build the assembly RemoteObject from the class RemoteObject 

2. ServerApp 

The remote object needs a server process where it will be instantiated. This server has to create a channel and put 
it into listening mode so that clients can connect to this channel. For creating server, Take .NET IDE-> New Project  named "Remoting.Server".This is console application.

 

Add RemoteObject.dll and Remoting as Reference

 

 

 

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using Remoting.RemoteObject;

 

 

namespace Remoting.Server

{

      /// <summary>

      /// Summary description for Class1.

      /// </summary>

      class Server

      {

            public Server()

            {

                  Console.WriteLine("Hello");

            }

            /// <summary>

            /// The main entry point for the application.

            /// </summary>

            [STAThread]

            public static int Main(string[] args)

            {

                  TcpChannel chan=new TcpChannel(8080);

                  ChannelServices.RegisterChannel(chan);

                  RemotingConfiguration.RegisterWellKnownServiceType(

                        typeof(Remoting.RemoteObject.RemoteObject),

                        "RemoteObject", WellKnownObjectMode.SingleCall);

                  System.Console.WriteLine("Hit  to exit…");

                  System.Console.ReadLine();

                  return 0;

            }

      }

}

Build it.It will create server.exe

 

3. Client App

For creating client, Take .NET IDE-> New Project  named "Client".This is console application.

Add RemoteObject.dll and Remoting as Reference

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using Remoting.RemoteObject;

 

namespace Remoting.Client

{

      /// <summary>

      /// Summary description for Class1.

      /// </summary>

      class Client

      {

            /// <summary>

            /// The main entry point for the application.

            /// </summary>

            [STAThread]

            public static int Main(string[] args)

            {

                  TcpChannel chan = new TcpChannel();

                  ChannelServices.RegisterChannel(chan);

                  Remoting.RemoteObject.RemoteObject  remObject = (RemoteObject.RemoteObject)Activator.GetObject(

                        typeof(Remoting.RemoteObject.RemoteObject),

                        "tcp://localhost:8080/RemoteObject");

 

                  if (remObject==null)

                        Console.WriteLine("cannot locate server");

                  else

                        remObject.ReplyMessage("You there?");

                  return 0;

            }

      }

}

 

Built it.

 

 

 

4. How to run it?

Console application as server and client: Run the Server.exe then run Client.exe. 

 

 

 

Advertisements

One thought on “How to use .Net Remoting in C#

  1. Needless to say, perform for magazines can consist of all kinds of points, although for me, my speciality is fiction, so for this examine, I will stick with what I know even though some of the rules apply to non fiction items as nicely.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s