Tuesday, April 15, 2014

Client Server Applicatin in C# Sample Code



.Net Remoting in C#




using System.Collections.Generic;
using System.Linq;
using System.Text;
using lab7_clas;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace clientside
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel());
            Class1 obj = (Class1)Activator.GetObject(typeof(Class1), "tcp://localhost:3128/HelloWorld");
            int count = 0;
            for (count = 0; count < 5; count++)
            {
                obj.Helloworld();
            }
        }
    }
}

Client







Server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using lab7_clas;
namespace lab7_1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpServerChannel channel = new TcpServerChannel(3128);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "HelloWorld", WellKnownObjectMode.SingleCall);
            Console.WriteLine("Press Enter to Stop Server");
            Console.ReadLine();
        }
    }
}




Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab7_clas
{
    public class Class1 : System.MarshalByRefObject
    {
        public Class1()
        {
            Console.WriteLine("This is the Constructor of Myclass");
        }
        public void Helloworld()
        {
            Console.WriteLine("A big Hello to the world");
        }
    }
}

Wednesday, March 19, 2014

Serialization And De-Serialization in C Sharp Using Arrays




Create a serializable class with two arrays. One for rate of the advertisement and other for running time of the advertisement. Serialize the object of this class to a stream using Soap formatter. Create a desktop application that asks the user for the start time and the number of minutes for which the advertisement has to be run. De-serialize the object from the stream and retrieve the values of the rates for specific time. Calculate the amount of money the user has to pay for running a particular advertisement.


using System;
usingSystem.Collections.Generic;
using System.IO;
usingSystem.Runtime.Serialization.Formatters.Soap;
usingSystem.Windows.Forms;
usingSystem.Xml.Serialization;

namespace WindowsFormsApplication2
{
    [Serializable]
publicpartialclassTaxRate : Form
    {
int[] array1 = newint[2];
int[] array2 = newint[2];
publicTaxRate()
        {
InitializeComponent();}
int a = 0;
privatevoid button3_Click(object sender, EventArgs e)
        {for (inti = 0; i< array1.Length; i++)
            {
if (array1[i] == Convert.ToInt32(UserSalarytextBox.Text))
                {a = array1[i] / 100 * array2[i];
                }
            }label4.Text = Convert.ToString(a);
        }
inti = 0;        
privatevoid button1_Click(object sender, EventArgs e)
        {
array1[i] = Convert.ToInt32(SalarytextBox.Text);
array2[i] = Convert.ToInt32(TaxtextBox.Text);
i++;
        }
privatevoid button2_Click(object sender, EventArgs e)
        {for (inti = 0; i< array1.Length; i++)
            {
Salary.Items.Add(array1[i]);
            }
for (inti = 0; i< array2.Length; i++){
Tax.Items.Add(array2[i]); } }
privatevoid button4_Click(object sender, EventArgs e)
        {
Stream stream1 = File.OpenWrite("D:\\binaryserialization1.txt");
Stream stream2 = File.OpenWrite("D:\\binaryserialization2.txt");
SoapFormattersf = newSoapFormatter();
sf.Serialize(stream1, array1);
sf.Serialize(stream2, array2);
stream1.Close();
stream2.Close();
        }privatevoid button5_Click(object sender, EventArgs e)
        {
FileStream stream1 = newFileStream("D:\\binaryserialization1.txt", FileMode.Open);
FileStream stream2 = newFileStream("D:\\binaryserialization2.txt", FileMode.Open);
SoapFormatterformate = newSoapFormatter();
            array1 = formate.Deserialize(stream1) asint[];
            array2 = formate.Deserialize(stream2) asint[];
for (inti = 0; i< array1.Length; i++)
            {
Salary.Items.Add(array1[i]);
            }
for (inti = 0; i< array2.Length; i++)
            {
Tax.Items.Add(array2[i]); }
 }
}
}



Monday, March 17, 2014

Serialization and De-Serialization in C Sharp




Create a Serializable class that stores tax rate and tax limit. Serialize the object of this class to a stream using Soap formatter. Create a desktop application that asks the monthly salary from the user and calculates the amount of tax he/she has to pay. De-serialize the object from the stream and get the value of tax limit that was stored. If the annual salary of the user exceeds the limit then calculate the amount of tax otherwise the user does not have to pay any tax.

using System;
usingSystem.Collections.Generic;
using System.IO;
usingSystem.Runtime.Serialization.Formatters.Binary;
usingSystem.Windows.Forms;
namespace WindowsFormsApplication2
{
    [Serializable]
publicpartialclassForm1 : Form
    {
public Form1()
        {
InitializeComponent();
        }
Dictionary<int, int>dic = newDictionary<int, int>();
privatevoid button1_Click(object sender, EventArgs e)
        {
dic.Add(Convert.ToInt32(TimetextBox.Text),Convert.ToInt32(RatetextBox.Text));
        }
privatevoid button2_Click(object sender, EventArgs e)
        {
foreach (var item indic)
            {
Times.Items.Add(item.Key);
Rates.Items.Add(item.Value);
            }
        }
int a = 0;
privatevoid button3_Click(object sender, EventArgs e)
        {
foreach (var item indic)
            {
if (Convert.ToInt32(textBox1.Text) == item.Key)
                {
                    a = item.Key * item.Value * 6;
                }
            }
            label4.Text = Convert.ToString(a);
        }
privatevoid button4_Click(object sender, EventArgs e)
        {
Streamstream = File.OpenWrite("D:\\binaryserialization1.txt");
BinaryFormatterfm = newBinaryFormatter();
fm.Serialize(stream,dic);
stream.Close();  }
privatevoid button5_Click(object sender, EventArgs e)
        {
FileStream stream = newFileStream("D:\\binaryserialization1.txt",FileMode.Open);
BinaryFormatterformate = newBinaryFormatter();

dic = formate.Deserialize(stream) asDictionary<int, int>;
foreach (var item indic)
            {
Times.Items.Add(item.Key);
Rates.Items.Add(item.Value);
            }
        }
    }
}