zoukankan      html  css  js  c++  java
  • .net remoting范例

    1:创建类库:

    using System;

    namespace RemoteHello
    {
     /// <summary>
     /// Hello 的摘要说明。
     /// </summary>

     public class Hello : System.MarshalByRefObject
     {
      public Hello()
      {
       //
       // TODO: 在此处添加构造函数逻辑
       //
       Console.WriteLine("Constructor called");
      }

      ~Hello()
      {
       Console.WriteLine("Destructor called");
      }

      public string Greeting(string name)
      {
       Console.WriteLine("Greeting called");
       return "Hello," + name;
      }
     }
    }

    2::创建一个工程文件,是SERVER端的。

    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;

    namespace HelloServer
    {
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
     class HS
     {
      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
       //
       // TODO: 在此处添加代码以启动应用程序
       //
       try
       {
        TcpServerChannel channel =new TcpServerChannel(8086);
        ChannelServices.RegisterChannel(channel);
        Type t=typeof(RemoteHello.Hello);   
        RemotingConfiguration.RegisterWellKnownServiceType(t,"hi",WellKnownObjectMode.SingleCall);
        Console.WriteLine("hit to exit");
        Console.ReadLine();
       }
       catch (Exception ex)
       {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.Source);
        Console.ReadLine();
       }
      }
     }
    }

    3:创建一个工程文件是client端的:

    using System;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;

    namespace HelloClient
    {
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
     class HC
     {
      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
       //
       // TODO: 在此处添加代码以启动应用程序
       //
       try
       {
        TcpClientChannel tc=new TcpClientChannel();
        ChannelServices.RegisterChannel(tc);
        RemoteHello.Hello obj = (RemoteHello.Hello)Activator.GetObject(typeof(RemoteHello.Hello),"tcp://10.10.10.111:8086/hi");
        if(obj==null)
        {
         Console.WriteLine("FAILED!");
         return;
        }
        for(int i=0;i<2;i++)
        {
         Console.WriteLine(obj.Greeting("haha"));
         Console.ReadLine();
        }
       }
       catch (Exception ex)
       {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.Source);
        Console.ReadLine();
       }   
      }
     }
    }

    3:在编译完以后,先运行server.exe文件,然后再运行client.exe文件,,,这样在MS-DOS下面,就可以看到,client掉用server。。。

    PS:1建立的类库,2,3建立的控制台程序.

    2.3必须引用1的DLL文件

    呵呵。。。完毕。。

  • 相关阅读:
    洛谷P6218 [USACO06NOV] Round Numbers S 题解 数位DP
    Duilib的双缓冲实现,附带GDI、WTL的双缓冲实现
    关于热键HotKey与WM_KEYDOWN的一点心得
    源码不匹配,找到了xxx的副本。
    SetForegroundWindow的失效问题: 跨进程的窗口前置。
    2021年4月的一份总结
    制作msi文件,exe转msi文件。
    内存限制
    ISPRS Vaihingen 数据集解析
    Linux中sudo、su和su -命令的区别小结
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1541013.html
Copyright © 2011-2022 走看看