C# .Net Remoting樣本

來源:互聯網
上載者:User

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

        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.