I'm currently experimenting the Ipc channel from the remoting tools provided by .NET framework.
For now I tested with success theses programs :
The server :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using RemotingTool;
namespace RemotingServer
{
class Program
{
static void Main(string[] args)
{
try
{
//Création du canal puis enregistrement sur le serveur
IpcChannel channel = new IpcChannel("localhost:9090");
ChannelServices.RegisterChannel(channel, false);
//Lancement de l'écoute avec l'object "Chat"
//Singleton => Une seule instance de l'objet partagée avec les clients
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Chat), "Chat", WellKnownObjectMode.Singleton);
Console.WriteLine("# Serveur en ligne # \n");
//Console.ReadLine();
}
catch
{
Console.WriteLine("# Erreur Serveur # \n");
Console.ReadLine();
}
}
}
}
The client :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using RemotingTool;
namespace RemotingClient
{
class Program
{
static void Main(string[] args)
{
try
{
//Création du canal + enregistrement sur le serveur
IpcChannel channel = new IpcChannel();
ChannelServices.RegisterChannel(channel, false);
//Ici on récupère l'instance de l'objet distant
Chat chat = (Chat)Activator.GetObject(typeof(Chat), "ipc://localhost:9090/Chat");
Console.WriteLine("# Connexion au serveur réussie # \n Saissisez votre message");
bool exit = false;
while(!exit)
{
string message = Console.ReadLine();
if(message != String.Empty)
{
try
{
if(chat!=null)
{
bool result = chat.TransfertMessage(message);
if(result)
{
Console.WriteLine("Le message à été transmis au serveur dood");
}
else
{
throw new Exception();
}
chat.IncrementValue();
}
}
catch
{
Console.WriteLine("Erreur communication serveur");
}
}
else
{
exit = true;
}
}
}
catch
{
Console.WriteLine("Erreur de connexion au serveur");
}
}
}
}
And the class of the shared object
using System;
namespace RemotingTool
{
public class Chat : MarshalByRefObject
{
public int value = 0;
public override object InitializeLifetimeService()
{
return null;
}
public bool TransfertMessage(string message)
{
Console.WriteLine(String.Format("Message reçu:{0}",message));
return true;
}
public void IncrementValue()
{
value++;
}
}
}
The thing is, now that I have successfully called a method of the shared object to be executed by the server, I would like to allow the client to change a variable or property of the object and then retrieve the new value from the server, by triggering an event if it is possible.
Do you have any advice on how to proceed ?