TCP Remoting
Example of using TCP remoting to send messages between processes: The code has a procedure that checks if server is present and port is opened, before sending the message (it might still happen that the port is opened by another application in which case the code will crash).
Project: IPC_SharedInterfaces Class: ICommunicationService
Public Interface ICommunicationService Sub SaySomething(ByVal text As String) End Interface
Project: IPC_Server Class: CommunicationService
Public Class CommunicationService Inherits MarshalByRefObject Implements IPC_SharedInterfaces.ICommunicationService Public Sub SaySomething(ByVal text As String) Implements IPC_SharedInterfaces.ICommunicationService.SaySomething MsgBox(text) End Sub End Class
Class: Form1
Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim ipcCh As IpcChannel Dim tcpCh As New Tcp.TcpServerChannel(31337) ChannelServices.RegisterChannel(tcpCh, False) RemotingConfiguration.RegisterWellKnownServiceType( GetType(CommunicationService), "MojURI", WellKnownObjectMode.SingleCall) End Sub End Class
References: IPC_SharedInterfaces, System.runtime.Remoting
Project: IPC_Client Class: Form1
Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim tcpCh As New Tcp.TcpClientChannel ChannelServices.RegisterChannel(ipcCh, False) Dim obj As IPC_SharedInterfaces.ICommunicationService = DirectCast(Activator.GetObject(GetType(IPC_SharedInterfaces.ICommunicationService), "tcp://localhost:31337/MojURI"), IPC_SharedInterfaces.ICommunicationService) If IsPortAvaliable(31337) Then obj.SaySomething(Now.TimeOfDay.ToString) Else MsgBox("Nema servera") End If ChannelServices.UnregisterChannel(tcpCh) End Sub Private Function IsPortAvaliable(port As Integer) As Boolean Dim RetVal As Boolean = False Dim globalProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties() Dim activeListeners As IPEndPoint() = globalProperties.GetActiveTcpListeners() For i As Integer = 0 To activeListeners.Length - 1 If activeListeners(i).Port = port Then RetVal = True Next Return RetVal End Function End Class
References: IPC_SharedInterfaces, System.runtime.Remoting