Srikanth Technologies

Creating a distributed application using .NET Remoting

.NET provides Remoting as one of the options to build distributed applications. Distributed application is an application that has two distinct parts - one runs on server and another runs on client. Server part of the application generally process data sent to it from client and client part takes care of user interface.

.NET Remoting is is one of the two options to develop a distributed application in .NET. The other option is Web service. Web services provide interoperability, which allows an application developed in .NET to be accessed from Java and vice-versa.

Though .NET Remoting restricts client and server to .NET, it provides better performance and flexibility regarding serialization (converting objects to bytes) and protocol used.

.NET Remoting supports connection based distributed application, which is not possible with Web services.

Prior to .NET, distributed applications on Windows were developed using ong>DCOM (Distributed Component Object Model).

Creating Remote Server

Take the following steps to create a simple remote server that sends a simple message requesting client. Client gets access to Hello object and calls GetMessage() method to receive message.
  1. Start VisuVisual Studio.NET.
  2. Select ong>File->New Project. Select Visual C# as language and Console Application in Template. Enter project name as Server.
  3. Add a class called Hello using Project->Add Class option. Hello class must be accessible from remote client, so it must inherit MarsMarshalByRefObject class.
  4. Provide a simple method called ong>GetMessage() as follows.
    using System;
    
    namespace Server
    {
        // class that is accessible to remote clients
        public class Hello : MarshalByRefObject     
        {
            public String GetMessage()
            {
                return "Hello From Server At Srikanth Technologies.";
            }
        }
    }
    
    
  5. Select project in Solution Explorer and Right click. Select Add Reference from context menu.
  6. Select .NET tab in Add Reference dialog box. Select System.Runtime.Remoting library and click on Ok.
  7. Rename class Program to HelloServer and write the following code.
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    
    namespace  Server
    {
        class HelloServer
        {
            static void Main(string[] args)
            {
                TcpChannel channel = new TcpChannel(9999);  // port no. 9999
                
                // Register channel 
                ChannelServices.RegisterChannel(channel,false);
    
                // Register as an available service with the name hello
                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(Hello),
                    "hello",
                    WellKnownObjectMode.SingleCall
                );
    
                System.Console.WriteLine("Press the enter key to exit...");
                System.Console.ReadLine();
            }  // end of main
        } // end of class
    } // end of namespace
    
    
  8. Build project using Build-> Build Solution
  9. Go to BIN\DEBUG folder of the project and run Server.Exe by double clicking on it. This invokes server. Server is now waiting for client to make request.
Let us now create a client project to call remote server.

Creating Remote Client

Client is a simple console application that gets access to remote object and then calls GetMessage() method. The following are the steps to create client application.
  1. Select File->New Project. Select Visual C# as language and Console Application in Template. Enter project name as Client.
  2. Select project in Solution Explorer and Right click. Select Add Reference from context menu.
  3. Select .NET tab in Add Reference dialog box. Select System.Runtime.Remoting library.
  4. Select Browser tab and select Server.exe file from BIN\DEBUG of Server project.
  5. Click on OK in Add Reference dialog box.
  6. Rename class Program to HelloClient and write the following code.
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    namespace Client
    {
        class HelloClient
        {
            public static void Main(string[] args)
            {
                TcpChannel channel = new TcpChannel();
                ChannelServices.RegisterChannel(channel,false);
    
                // Get an instance of the remote object
                Server.Hello  obj = ( Server.Hello)Activator.GetObject(
                    typeof(Server.Hello),  
                    "tcp://localhost:9999/Hello"
                );
    
                // Use the object
                Console.WriteLine(obj.GetMessage());
            }
        }
    }
    
  7. Build project using Build->Build Solution.
  8. Run the project using CTRL + F5. You must see a message coming from server.

Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :

Comments