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; } } } }
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]; } } }
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(); } } }
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); } } }