如何在.NET框架中使用IPC交换数据

发布于 2025-02-09 20:32:04 字数 4103 浏览 2 评论 0原文

目前,我正在通过.NET Framework提供的远程工具来实验IPC频道。 目前,我通过成功的程序进行了测试:
服务器:

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();
            }

            
        }
    }
}

客户端:

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");
            }
        }
    }
}

以及共享对象的类

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++;
        }

    }
}

是事实,现在我成功地称为服务器执行的共享对象的方法,我想允许客户端更改可变或属性对象,然后从服务器中检索新值,并在可能的情况下触发事件。

您对如何进行有任何建议吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文