There are plenty of articles around for inter process communication, using remoting, custom windows messages and named pipes. In my .net career I think I’ve used all of them. In this article I’ll outline a technique I’ll be using that I think is quite simple.
In .net Framework 2.0, Microsoft introduced the new System.Runtime.Remoting.Channels.Ipc remoting channel , which provides one of the easiest (prior to using WCF) techniques.
In this example we have a single server process which is communicated to via client processes.
SharedInterfaces
Firstly, as with all remoting it is necessary to share a type that defines the api you desire between client and server processes. One of the easiest things to do is to create an interface.
For the sake of this example, I created an assembly called “Shared Interfaces” containing an interface called “ICommunicationService”:
Public Interface ICommunicationService
Sub SaySomething(ByVal text As String)
End Interface
Server
For the Server in this example, I created a console application (it can be any sort of process).
The Server project contains two classes:
- An implementation of ICommunicationService called “CommunicationService”
- A Main that registers to CommunicationService class for acess
The CommunicationService class implements the interface and inherits from MarshalByRefObject :
Public Class CommunicationService
Inherits MarshalByRefObject
Implements SharedInterfaces.ICommunicationService
Public Sub SaySomething(ByVal text As String) Implements SharedInterfaces.ICommunicationService.SaySomething
Console.WriteLine("The client said : " & text)
End Sub
End Class
The main registers the type:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Ipc
Module Main
Sub Main()
Dim ipcCh As IpcChannel
ipcCh = New IpcChannel("IPChannelName")
ChannelServices.RegisterChannel(ipcCh, False)
RemotingConfiguration.RegisterWellKnownServiceType( _
GetType(CommunicationService), "SreeniRemoteObj", _
WellKnownObjectMode.Singleton)
Console.WriteLine("Press ENTER to quit")
Console.ReadLine()
End Sub
End Module
Client
The client project is written as a Winform with a textbox and a button. The button click contains this code:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Ipc
Public Class Form1
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
Dim ipcCh As New IpcChannel("myClient")
ChannelServices.RegisterChannel(ipcCh, False)
Dim obj As SharedInterfaces.ICommunicationService = _
DirectCast(Activator.GetObject(GetType(SharedInterfaces.ICommunicationService), _
"ipc://IPChannelName/SreeniRemoteObj"), SharedInterfaces.ICommunicationService)
obj.SaySomething(txtText.Text)
ChannelServices.UnregisterChannel(ipcCh)
End Sub
End Class
The code basically gets a reference to the remote object and invokes the “SaySomething” message, passing through a value.
As you can see, it doesn’t take much code to communicate between processes.
Improvements
Further improvements to the code will involve ensuring security (making sure that only desirable client processes are calling).
Links