Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

Simple Sample for .NET Remoting

Rate me:
Please Sign up or sign in to vote.
4.19/5 (10 votes)
14 Feb 2007CPOL3 min read 62.3K   3.5K   38  
This article is for beginners
using System;
using System.Windows.Forms;
using System.Configuration;
//RemoteOject is defined in this namespace
using Sample.RemoteObject;
//Used for remoting
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


namespace Server
{
    /// <summary>
    /// Used as a Server where the remote component is registered and made available to the clients
    /// </summary>
    public partial class Listener : Form
    {
        //Tcp Channel is being used for communicating with the clients
        private TcpChannel _serverChannel;
        private int _port;
        public Listener()
        {
            InitializeComponent();
            //Getting the port defined in the configuration file
            _port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);
            lblPort.Text = Convert.ToString(_port);
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            if (_serverChannel == null)
            {
                //Registering the tcp channel
                _serverChannel = new TcpChannel(_port);
                ChannelServices.RegisterChannel(_serverChannel);
                //Registering the server component as a server activated object (SOA)
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteCalculator), "RemoteCalculator", WellKnownObjectMode.Singleton);
                btnListen.Text = "Stop Listening";
            }
            else
            {
                btnListen.Text = "Start Listening";
                //Unregistering the tcp channel so that server will not be available here after
                ChannelServices.UnregisterChannel(_serverChannel);
                _serverChannel = null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions