zoukankan      html  css  js  c++  java
  • C# .Net Remoting示例

     IPCChannel.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel

     

    IPC(Inter-Process Communication,进程间通信)

     /Files/wucg/_TestProjects/TestNetRemoting_IPC通信.rar

    eg1:

    server端

    namespace DotNetRemotingDemo2Server
    {
        class Program
        {
            static void Main(string[] args)
            {
                RemotingConfiguration.Configure("DotNetRemotingDemo2Server.exe.config",false);
                Console.ReadLine();
            }
        }
    }

    配置:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>
      <system.runtime.remoting>
        <application name="RemoteServer">
          <service>
            <!--<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="Singleton" />-->        
            <wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="SingleCall" />        
          </service>
          <channels>
            <channel ref="ipc" portName="testPipe" />
          </channels>
        </application>
      </system.runtime.remoting>
    </configuration>

    Client端:
    namespace DotNetRemotingDemo2Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                RemoteHelloConsole.Hello hello =
                    (RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
                "ipc://testPipe/Object.MyObject");

                try
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Console.WriteLine(hello.Greeting("abc123" + i));
                    }
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
        }
    }

    eg2:

    Server端:

    namespace DotNetRemotingDemo1
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpServerChannel channel = new TcpServerChannel(8086);
                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.RegisterWellKnownServiceType(
                    //typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.SingleCall);
                    typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.Singleton);
                Console.WriteLine("press return to exit.");
                Console.ReadLine();
            }
        }
    }

    Client端:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;


    namespace DotNetRemotingClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                ChannelServices.RegisterChannel(new TcpClientChannel(), false);

                RemoteHelloConsole.Hello hello = (RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
                    "tcp://localhost:8086/Hi");
                if (hello == null) {

                    Console.WriteLine("can't locate server.");
                    return;
                }
                try
                {
                    for (int i = 0; i < 5; i++)
                    {
                       Console.WriteLine( hello.Greeting("abc123" + i) );
                    }
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();

            }
        }
    }

  • 相关阅读:
    django模板引擎自定义变量
    Ubuntu系统桌面任务栏和启动器全部消失解决方案
    ubuntu beyond compare到期后续期
    git 分支操作 与 远程新建分支后,本地查看不到
    jmeter 安装
    ubuntu apt-get install 时报错curl : Depends: libcurl4 (= 7.58.0-2ubuntu3.6) but 7.61.0-1ubuntu2 is to be installed或者 vim : Depends: vim-common (= 2:8.0.1453-1ubuntu1) but 2:8.0.1766-1ubuntu1 is to be ins
    【uWSGI】 listen queue of socket (fd: 3) 错误分析
    通过CONN_MAX_AGE优化Django的数据库连接
    Linux(CentOS) 查看当前占用CPU或内存最多的K个进程
    centos django Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
  • 原文地址:https://www.cnblogs.com/wucg/p/1731592.html
Copyright © 2011-2022 走看看