간단한 Remoting 실천

9436 단어 in

한마디로 요약하다


remoting은 마이크로소프트의 일종의 실현이 서로 다르다.net 응용 프로그램에서 분포식 통신을 하는 기술

중요 개념


원리는 대체로 클라이언트가remoting 채널을 통해 서버 대상 에이전트를 얻고 서열화와 반서열 방식으로 데이터의 상호작용을 실현하는 것이다
원격 대상: 서버 측의 실현 클래스는MarshalByRefObject를 계승하여remoting 통신을 실현하고 프로그램 영역에 대한 접근을 지원해야 한다.
원격 객체 활성화
주로 서버 측 활성화와 클라이언트 활성화로 나뉜다
상호작용 전에 상응하는 실례를 활성화하여 방법 채널을 호출하기 편리하게 해야 한다. 주요 tcp, http, ipc 등 몇 가지 방식은 tcp가 2진법으로 전송되고 전송 효율이 높으며 랜에서 tcp를 사용하기에 적합하다.
http: soap 형식으로 메시지 대상을 서열화하여 방화벽을 뛰어넘을 수 있고 안전성이 높습니다.
IpcChannel: 프로세스 간 통신, 같은 시스템 프로세스 간 통신만 사용하며 호스트 이름과 포트 번호가 필요하지 않습니다.Http 채널과 Tcp 채널을 사용하면 호스트 이름과 포트 번호를 지정해야 합니다.

단순 데모


  1.서버 사이드 서비스 구현 클래스 만들기
 public class MyRemotingObject : MarshalByRefObject

    {

        //  Tcp  

        public int AddForTcpTest(int a, int b)

        {

            return a + b;

        }



        //  Http 

        public int MinusForHttpTest(int a, int b)

        {

            return a - b;

        }



        //  IPC 

        public int MultipleForIPCTest(int a, int b)

        {

            return a * b;

        }

    }

  2.서버repting 서비스 설정
<?xml version="1.0" encoding="utf-8" ?>

<!-- App.config -->

<configuration>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

  </startup>

  <system.runtime.remoting>

    <application>

      <service>

        <wellknown  mode="Singleton"

                    type="RemotingDemo.MyRemotingObject,RemotingDemo"

                    objectUri="MyRemotingObject"/>

      </service>

      <channels>

        <channel port="9001" ref="tcp"/>

        <channel port="9002" ref="http"/>

        <channel portName="IpcTest" ref="ipc"/>

        <!--Ipc -->

      </channels>

    </application>

  </system.runtime.remoting>

</configuration>

 3.서버 사이드 프로그램 입구에서 설치 프로필을 시작하여 내부에서 스스로 시작 서비스를 등록하도록 합니다
 RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);

4. 클라이언트가 원격 대상을 활성화하고 해당하는 방법을 호출
   MyRemotingObject proxyobj1 = Activator.GetObject(typeof(MyRemotingObject), "tcp://localhost:9001/MyRemotingObject") as MyRemotingObject;

            if (proxyobj1 == null)

            {

                Console.WriteLine(" TCP ");

            }



            //HttpChannel httpChannel = new HttpChannel();

            //ChannelServices.RegisterChannel(httpChannel, false);

            MyRemotingObject proxyobj2 = Activator.GetObject(typeof(MyRemotingObject), "http://localhost:9002/MyRemotingObject") as MyRemotingObject;

            if (proxyobj2 == null)

            {

                Console.WriteLine(" Http ");

            }



            //IpcChannel ipcChannel = new IpcChannel();

            //ChannelServices.RegisterChannel(ipcChannel, false);

            MyRemotingObject proxyobj3 = Activator.GetObject(typeof(MyRemotingObject), "ipc://IpcTest/MyRemotingObject") as MyRemotingObject;

            if (proxyobj3 == null)

            {

                Console.WriteLine(" Ipc ");

            }

            //  

            Console.WriteLine("This call object by TcpChannel, 100 + 200 = {0}", proxyobj1.AddForTcpTest(100, 200));

            Console.WriteLine("This call object by HttpChannel, 100 - 200 = {0}", proxyobj2.MinusForHttpTest(100, 200));

            Console.WriteLine("This call object by IpcChannel, 100 * 200 = {0}", proxyobj1.MultipleForIPCTest(100, 200));

            Console.WriteLine("Press any key to exit!");

            Console.ReadLine();

총결산


예전에는 관련 기술을 이해하는 것을 두려워해서 배우기가 매우 어려웠다. 예를 들어 wcf, 다음에 나는 wcf가 가져온 즐거움을 실천해야 한다!

좋은 웹페이지 즐겨찾기