首页 > 开发 > .Net > 正文

.net remoting范例

2020-02-03 16:01:35
字体:
来源:转载
供稿:网友
 

1:创建一个工程文件,是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();
   }
  }
 }
}

2:创建一个工程文件是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。。。

呵呵。。。完毕。。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表