Click here to Skip to main content
11,925,292 members (56,341 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked

Remote methods and events in C#

, 6 Sep 2003
Rate this:
Please Sign up or sign in to vote.
An article on Remoting in C#


This article gives and introduction to remote methods and events using c#.There are many articles already available on Remoting. Most of them deal with the remote methods .This article explains the implementation of a remote event also. This might be of use for beginners.


Microsoft knowledge Base Article -312114 Explains the changes required for the implementation of remote events. Here I am trying to provide a simpler explanation for the implementation of an event (call back) using the same article as my reference Smile | :)

The component

First we have to create a component which has a method and an event available in it. A new c# project is created as a class library .The component code is added there. The component hosts a method an an event handler as shown below. A global Delegate should also be declared representing the type of the eventhandler. The event handler will hold the callback function pointer. The clients will be subscribing and unsubscribing to this.

 public delegate void myeventhandler(string str);

//component defenition is as below

  public  abstract class AbstractServer : MarshalByRefObject
    public abstract string myfunc(string what);
    public abstract event  myeventhandler myevent;

Also a class that will have the implementation in the client has to be defined as below

public abstract class AbstractBroadcastedMessageEventSink :
  public void myCallback(string str)
  protected abstract void internalcallback (string str) ;


The server should reference to this component. Here in the source i have done the implementation of the component in the server side. The EventHandler that enables the Subscribing of the client method has to be implemented. Also an internal method FireNewBroadcastedMessageEvent is being included .This method will call the actual call back which the client would have subscribed.

Apart from that the server should also do the functionalities like , open a Tcp Channel, Register that Tcp channel and then register the componet as a well known Service type.T he server used here is a console application. The code that does these functions is as shown below.

using System;
using mycomponent;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
  class Class1
    static void Main(string[] args)
      TcpChannel m_TcpChan = new TcpChannel(9999); //open a channel


      Type theType = new  ServerClass().GetType();  
      System.Console.WriteLine("Press ENTER to quit");

  //component implementation
    public class ServerClass : AbstractServer
      public override string myfunc(string what)
      Console.WriteLine("in myfunc");
      FireNewBroadcastedMessageEvent("Event: " + what + " was said");
      return "done";

      public event  myeventhandler myHandler;

      public override event myeventhandler myevent
        Console.WriteLine("in event myevent + add");
        myHandler = value;

        Console.WriteLine("in event myevent + remove");

      protected void FireNewBroadcastedMessageEvent(string text)



The client should first define a class( as EventSink) that implements the "AbstractBroadcastedMessageEventSink " class that was defined in the component.

In normal remoting if we do not need the callbacks, the client has to register a tcpchannel and then initiate the object using the port number and ip address of the remote pc. But as there are call backs ,this client should also open a TCp channel with a different port number. It should then register that TCP channel and also register the EventSink as a wellknown service type .This is because we use the internal method of the EventSink as our callback method .If we do not perform the above operations our callback will not be called. We set the "myevent" handler of the object to the "sink.callback" function. So when we execute the "FireNewBroadcastedMessageEvent" the method "myHandler("hai")" will be called and this internally calls our call back.

using System;
using mycomponent;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
  class EventSink : AbstractBroadcastedMessageEventSink
    protected override  void internalcallback (string str) 
      Console.WriteLine("Your message in callback ");


  class Class1
    static void Main(string[] args)
      TcpChannel m_TcpChan = new TcpChannel(1011);

      AbstractServer m_RemoteObject = (AbstractServer)

  //here SANJEEV is my PC name .Here the pcname of the machine 
  //where the server is running has to be given
  //FirstRemore is the name that was given in the 
  //registerwellknownservicetype from the server       


      EventSink sink = new EventSink();
      m_RemoteObject.myevent += new myeventhandler(sink.myCallback);



Start the Server. Then Start the client. If the call back method gets called. Then Success!!!


I'm posting this article as i found it very difficult to find a simple article on remote events. Hope this will help somebody who is searching for code on remote events . This is the first article that I am posting on this site. Thank you for the time. Smile | :)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
I am Sanjeev Krishnan.

You may also be interested in...

Comments and Discussions

QuestionWhat? Pin
Moxxis28-Sep-11 19:05
memberMoxxis28-Sep-11 19:05 
AnswerRe: What? Pin
Moxxis28-Sep-11 22:46
memberMoxxis28-Sep-11 22:46 
Generalnotify the client if the server ip address changes Pin
saisunil_d13-Jan-09 11:56
membersaisunil_d13-Jan-09 11:56 
GeneralRe: notify the client if the server ip address changes Pin
Moxxis28-Sep-11 22:18
memberMoxxis28-Sep-11 22:18 
GeneralUnhandled Exception: System.IO.FileNotFoundException Pin
Member 555299426-Sep-08 3:13
memberMember 555299426-Sep-08 3:13 
GeneralConfigure Time to live Pin
manaf11-Jul-05 5:59
membermanaf11-Jul-05 5:59 
GeneralRe: Configure Time to live Pin
igal.p5-Dec-05 3:27
memberigal.p5-Dec-05 3:27 
QuestionHow about a GUI example Pin
secovel12344-Apr-05 9:43
susssecovel12344-Apr-05 9:43 
Why are all the examples I can find console applications? I need to see an example where the event returned from the server then goes on to update the GUI, not Console.WriteLine. How about textBox1.AppendText(msg); There is something very different about the scenario, but I can't seem to figure out what, except that it locks up the client.

AnswerRe: How about a GUI example Pin
fmccown30-Apr-05 3:47
memberfmccown30-Apr-05 3:47 
GeneralUsing retrieved data in the client Pin
RyanHilton27-Jan-05 12:14
memberRyanHilton27-Jan-05 12:14 
GeneralRe: Using retrieved data in the client Pin
Jacob Anderson8-Feb-06 8:23
memberJacob Anderson8-Feb-06 8:23 
GeneralWhy the two methods Pin
42Kent429-Nov-04 13:06
member42Kent429-Nov-04 13:06 
GeneralRe: Why the two methods Pin
mikeperetz11-Jan-05 16:42
membermikeperetz11-Jan-05 16:42 
GeneralRe: Why the two methods Pin
Moxxis28-Sep-11 22:27
memberMoxxis28-Sep-11 22:27 
GeneralHere are fixes to get this working!! Pin
bducks8-Apr-04 20:49
sussbducks8-Apr-04 20:49 
GeneralRe: Here are fixes to get this working!! Pin
bducks8-Apr-04 21:37
sussbducks8-Apr-04 21:37 
GeneralRe: Here are fixes to get this working!! Pin
Member 152309417-Aug-08 5:52
memberMember 152309417-Aug-08 5:52 
GeneralRe: Here are fixes to get this working!! Pin
KiwiRoo11-Apr-10 21:37
memberKiwiRoo11-Apr-10 21:37 
Questionwindows forms type server? Pin
bitvadasz9042-Mar-04 22:42
memberbitvadasz9042-Mar-04 22:42 
AnswerRe: windows forms type server? Pin
bducks8-Apr-04 20:33
sussbducks8-Apr-04 20:33 
GeneralThe Configuration Pin
Anonymous20-Feb-04 8:52
sussAnonymous20-Feb-04 8:52 
GeneralRe: The Configuration Pin
manaf10-Jul-05 1:18
membermanaf10-Jul-05 1:18 
GeneralThank You Pin
jparsons21-Oct-03 10:59
memberjparsons21-Oct-03 10:59 
Generalmultiple clients Pin
krdoki@email.ro8-Oct-03 5:58
memberkrdoki@email.ro8-Oct-03 5:58 
GeneralRe: multiple clients Pin
Chris Gibbons14-May-04 13:53
sussChris Gibbons14-May-04 13:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 7 Sep 2003
Article Copyright 2003 by san77in
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid