Currency conversion server and client using .NET Remoting

In this blog, I will show how to create a .NET Remoting server that converts the amount in INR (Indian Rupee) to the given currency. The main objective is to show how to build a .NET Remoting server and client.

Creating CurrencyServer

Create a new Console application using C# and create the following code in that project. Make sure you add System.Runtime.Remoting library to the project.

CurrencyInfo.cs

This is a serializable class that is used to send data from client to server.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CurrencyServer
{
    [Serializable]
    public  class CurrencyInfo
    {
        private double amount;
        public double Amount
        {
            get { return amount; }
            set { amount = value; }
        }
        private string symbol;
        public string Symbol
        {
            get { return symbol; }
            set { symbol = value; }
        }
    }
}

CurrencyConverter.cs

This is the remote object that is made available to client. It contains a set of methods to convert the given amount to the target currency based on the symbol.
using System;
using System.Collections.Generic;

namespace CurrencyServer
{
    public class CurrencyConverter : MarshalByRefObject 
    {
        Dictionary<String, double> rates = new Dictionary<string, double>();

        public CurrencyConverter()
        {
            // initialize rates with hypothetical data
            rates.Add("usd", 48.12);
            rates.Add("ukp", 75.98);
            rates.Add("aud", 23.55);
        }

        public double Convert(double amount)
        {
            return amount / rates["usd"];  // default symbol usd is used
        }

        public double Convert(double amount, string tocurrency)
        {
            if (rates.ContainsKey(tocurrency))
                return amount / rates[tocurrency];
            else
                return -1;
        }

        public double Convert(CurrencyInfo info)
        {
            return info.Amount / rates[info.Symbol];
        }
    }
}

CurrencyServer.cs

This class contains Main function to register the remote object. It uses port number 8888 and name currency for remote object.

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;

namespace CurrencyServer
{
    public class CurencyServer
    {
        static void Main(string[] args)
        {
            TcpChannel ch = new TcpChannel(8888);
            ChannelServices.RegisterChannel(ch, false);

            RemotingConfiguration.RegisterWellKnownServiceType(
                   typeof(CurrencyConverter),
                   "currency",
                   WellKnownObjectMode.SingleCall);

            Console.WriteLine("Currency Server Is Ready..");
            Console.ReadLine();
        }
    }
}
Build the project with these classes and run the project (currencyserver.exe) from command prompt.

Creating Client

Create the client as a windows application. Create a new project and place the controls shown as below. Write the following code for Form1.cs.

Use Add References option from project menu and add System.Runtime.Remoting assembly and CurrencyServer.exe assembly from CurrencyServer\bin\debug folder.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;

namespace WinCurrencyClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnConvert_Click(object sender, EventArgs e)
        {
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);

            // Get an instance of the remote object
            CurrencyServer.CurrencyConverter c =  (CurrencyServer.CurrencyConverter)
              Activator.GetObject(
                             typeof(CurrencyServer.CurrencyConverter), 
                             "tcp://localhost:8888/currency");

            double amount = c.Convert(
                Double.Parse(txtAmount.Text),  
                cmbToCurrency.SelectedItem.ToString());

            if (amount < 0)
                MessageBox.Show("Sorry! Convertion Failed", "Error");
            else
               MessageBox.Show( amount.ToString(), "Amount");

            ChannelServices.UnregisterChannel(channel);

        }

        private void btnConvert2_Click(object sender, EventArgs e)
        {
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);

            // Get an instance of the remote object
            CurrencyServer.CurrencyConverter c = (CurrencyServer.CurrencyConverter)
              Activator.GetObject(
                              typeof(CurrencyServer.CurrencyConverter),
                             "tcp://localhost:8888/currency");

            CurrencyServer.CurrencyInfo cinfo = new CurrencyServer.CurrencyInfo();
            cinfo.Amount = Double.Parse(txtAmount.Text);
            cinfo.Symbol = cmbToCurrency.SelectedItem.ToString();

            double  amount = c.Convert(cinfo);

            MessageBox.Show(amount.ToString(), "Converted Amount");
            ChannelServices.UnregisterChannel(channel);
        }
    }
}


Home    Blogs

Post Your Comment

Image

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

Comments


Posted By sindhu On 05-Jun-09 05:30:01 PM

Sir, why should we serialize if every thing is doing good.
We can work even if we dont have CurrencyInfo know sir...



Posted By Aloe Vera Distributor On 12-Oct-09 12:12:43 AM

Good night .
I have something to say, about aloevera, something about which you wrote above, about health and weight loss management... For a long time, I and my friend use the products of the forever living products. We each time see the good results and also we earn money for our families and we are happy. My friend works with aloe vera in the company of Forever Living Products has more than 5 years (My friend works ONLY in the forever living and has a wife and three children). I know aloe products for weight management for a long time, but a year ago, began working as a distributor in the Forever Living Products.
Of course, job is so hard, but in no other case, you really can earn so much money with so much fun and good smiling faces around...
So if you want to discuss something about your post, and about my experience with aloevera products for weight loss, I'll always be glad to talk.

Best regards from Washington and have a nice day!
Andrew - Aloe Vera Distributor
P.S. srikanthtechnologies.com - THE BEST!!!

Copyright © Srikanth Technologies. All rights reserved.