Srikanth Technologies

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 write 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! Conversion 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);
        }
    }
}

Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your 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...