Click here to Skip to main content
12,954,522 members (64,921 online)
Click here to Skip to main content


266 bookmarked
Posted 19 Apr 2006

Two-way Remoting with Callbacks and Events, Explained

, 19 Jul 2006 CPOL
A demonstration of how separate applications on different machines are able to effectively communicate with one another.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Serialization.Formatters.Binary;

namespace RemotingServerClient
    public partial class Form1 : Form
        public Form1()

        private ServerTalk _ServerTalk = null;      // this object lives on the server
        private CallbackSink _CallbackSink = null;  // this object lives here on the client

        private void btnRegister_Click(object sender, EventArgs e)
            // creates a client object that 'lives' here on the client.
            _CallbackSink = new CallbackSink();

            // hook into the event exposed on the Sink object so we can transfer a server 
            // message through to this class.
            _CallbackSink.OnHostToClient += new delCommsInfo(CallbackSink_OnHostToClient);

            // Register a client channel so the server can communicate back - it needs a channel
            // opened for the callback to the CallbackSink object that is anchored on the client!
            HttpChannel channel = new HttpChannel(9003);
            ChannelServices.RegisterChannel(channel, false);

            // now create a transparent proxy to the server component
            object obj = Activator.GetObject(typeof(ServerTalk), "http://marcelxp:9000/TalkIsGood");

            // cast returned object
            _ServerTalk = (ServerTalk)obj;

            // Register ourselves to the server with a callback to the client sink.
            _ServerTalk.RegisterHostToClient(this.txtUserID.Text, new delCommsInfo(_CallbackSink.HandleToClient));

            // make sure we can't register again!
            btnRegister.Enabled = false;    

        void CallbackSink_OnHostToClient(CommsInfo info)
            if (this.txtFromServer.InvokeRequired)
                this.txtFromServer.Invoke(new delCommsInfo(CallbackSink_OnHostToClient), new object[] { info });
                this.txtFromServer.Text = "From server: " + info.Message + Environment.NewLine + this.txtFromServer.Text;

        private void btnSend_Click(object sender, EventArgs e)
            _ServerTalk.SendMessageToServer(new CommsInfo(this.txtToServer.Text));
            this.txtFromServer.Text = "To server: " + this.txtToServer.Text + Environment.NewLine + this.txtFromServer.Text;



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.


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


About the Author

marcel heeremans
Software Developer (Senior)
United Kingdom United Kingdom
Ever since my dad bought me a Commodore 64 (some years back) I have been hooked to programming. For most of my working career I have worked intensely with C# (WinForms and WPF) but a few years back I started to investigate browser technologies and frameworks which are much more powerful than I thought at first!

I studied International Marketing but found the IT sector much more challenging. So straight from uni I took on some IT contract work in London in a buoyant market and never looked back.

If you wish to contact me then please do so on

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 19 Jul 2006
Article Copyright 2006 by marcel heeremans
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid