Click here to Skip to main content
15,886,761 members
Articles / Programming Languages / C#
Article

Inter-process communication via Remoting

Rate me:
Please Sign up or sign in to vote.
2.89/5 (8 votes)
14 Oct 2007CPOL 47.4K   1K   34   5
Inter-process communication by using Remoting.

Introduction

Different technologies can be used for communication with a client and a server application. We can program our applications by using sockets, or we can use some helper classes from the System.Net namespace that makes it easier to deal with protocols, IP addresses, and port numbers. .NET applications work within an application domain. An application domain can be seen as a subprocess within a process. Traditionally, processes are used as an isolation boundary. An application running in one process cannot access and destroy memory in another process. Cross-process communication is needed for applications to communicate with each other. With .NET, the application domain is the new safety boundary inside a process, because the MSIL code is type-safe and verifiable.

Screenshot - a3.jpg

Remoting communication library

C#
public class DataEventRepeator:MarshalByRefObject
{
   public event DataReceiveHandler ReceiveData;
   public void OnReceiveData(object sender, ReceiveDataEventArgs e)
   {
       if (this.ReceiveData!=null)
       {
           ReceiveData(this, e);
       }
   }
   
}
public class DataEventRepeators : List<DataEventRepeator>
{ }

DataServer communication library

C#
public class DataServer:MarshalByRefObject ,IDataReceiver
{
    public event DataReceiveHandler ReceiveData;
    private static DataEventRepeators reapters;
    public static DataEventRepeators Reapters
    {
        get
        {
            if (reapters==null)
            {
                reapters = new DataEventRepeators();
            }
            return DataServer.reapters; 
        }
    }
    public void SendData(string rec_data)
    {
        ReceiveDataEventArgs e = new ReceiveDataEventArgs();
        e.Data = rec_data;
        try
        {
            OnReceiveData(e);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
      
    }
    private void OnReceiveData(ReceiveDataEventArgs e)
    {
        if (ReceiveData!=null)
        {
            ReceiveData(null, e);
        }
    }
    public void AddEventRepeater(DataEventRepeator repeater)
    {
        Reapters.Add(repeater);
        this.ReceiveData += new DataReceiveHandler(repeater.OnReceiveData);
      
    }
   
}

Initial communication when first starting application via ProcessComLib.dll

C#
public void InitCommunication()
{
    BinaryServerFormatterSinkProvider provider = 
            new BinaryServerFormatterSinkProvider();
    provider.TypeFilterLevel = TypeFilterLevel.Full;
    IDictionary props = new Hashtable();
    props["port"] = 0;
    //Reg Tcp channel 
    TcpChannel chn = new TcpChannel(props, null, provider);
    ChannelServices.RegisterChannel(chn, false);
    repeater.ReceiveData += new DataReceiveHandler(repeater_ReceiveData);
    try
    {
        IDictionary prop = new Hashtable();
        prop["port"] = 2007;
        TcpChannel chnn = new TcpChannel(prop,null,provider);
        ChannelServices.UnregisterChannel(chn);
        ChannelServices.RegisterChannel(chnn, false);
        RemotingConfiguration.RegisterWellKnownServiceType
        (
        typeof(DataComServer.DataServer),
        "DataServer.rem",
        WellKnownObjectMode.Singleton
        );
     
    }
    catch
    {
        //tcp port has been registered
    }
    Connection();
}
private void Connection()
{
    //connect server
    dataServer = (IDataReceiver)Activator.GetObject(typeof(IDataReceiver), 
                  "tcp://localhost:2007/DataServer.rem");
    //add repter to EventRepeaterList
    dataServer.AddEventRepeater(repeater);
}

OK. That is all. More details can be found in the downloadable source code.

License

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


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

Comments and Discussions

 
GeneralNot working after 5 minuts Pin
Nobels4-Jun-10 20:24
Nobels4-Jun-10 20:24 
GeneralInteresting code, decent example, does what it's supposed to... Pin
Roger Venable15-Jun-09 5:37
Roger Venable15-Jun-09 5:37 
...but not much on the explanation.   The first instance of the process opens a listener on a port, subsequent instances listen on their own dynamic ports and hear echoes of the message repeated from the first instance.   It needs a protocol to exchange the instance node information, otherwise death of the first instance cripples the network (subsequent instance nodes are not aware of each other).   This would also facilitate code for recovery from network splits and disconnections.

I wouldn't mind seeing a revisited example of this which balances nodes into a tree and handles a great many number of nodes.
GeneralGood article Pin
Golllli11-May-09 7:30
Golllli11-May-09 7:30 
Generalre:zip file does not open Pin
miotan15-Oct-07 14:53
miotan15-Oct-07 14:53 
QuestionAttached zip not working [modified] Pin
Jon_Nichols15-Oct-07 6:54
Jon_Nichols15-Oct-07 6:54 

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.