当前位置:开发者网络 >> 技术教程 >> .NET教程 >> .NET Framework >> 内容
精彩推荐
分类最新教程
分类热点教程
  
.net remoting范例
作者:未知
日期:2005-05-26
人气:
投稿:(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:
 

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。。。

呵呵。。。完毕。。

相关文章: