Remoting实例(同为客户端服务端)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
  
          class LibraryClass : MarshalByRefObject
        {
            public LibraryClass()
                : base()
            {
                Console.WriteLine("远程对象被创建!");
            }
            ~LibraryClass()
            {
                Console.WriteLine("远程对象被析构!");
            }
          public  void SayHello(string name)
            {
                Console.WriteLine("你好,{0}", name);
            }
            public int GetSomthing(string s)
            {
                if (s != null)
                {
                    Console.WriteLine("我执行了!");
                    return s.Length;
                }
                return -1;
            }
        }
   
}






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime .Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

namespace test
{
    class RemotingServer
    {
      
            /// <summary>
            /// 服务端应用
            /// </summary>
            /// <param name="args"></param>
        public static void Entry()
        {
            //利用TCP通道,并侦听12345端口
            TcpServerChannel channel = new TcpServerChannel(10007);
            ChannelServices.RegisterChannel(channel, true);
            //使用WellKnown激活方式,并且使用SingleCall模式
           //“LibraryClass”可替换为  "li"                          LibraryClass li=new LibraryClass();
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(LibraryClass ), "LibraryClass", WellKnownObjectMode.SingleCall);//" LibraryClass"可替换为实例对象名
           
        }
       



    }
}










using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
namespace test
{
    class RemotingClient
    {
        public static  void  main()
       {
            try
            {
                //使用TCP通道连接
                TcpClientChannel channel2 = new TcpClientChannel("channel2", new BinaryClientFormatterSinkProvider());
                ChannelServices.RegisterChannel(channel2, true);
                //获取远程对象
               LibraryClass class1 = (LibraryClass)Activator.GetObject(typeof(LibraryClass ), "tcp://localhost:10007/LibraryClass");// LibraryClass路径可替换为实例对象名li
                //调用远程对象的方法
                class1.SayHello("DebugLZQ");
                class1.SayHello("http://www.cnblogs.com/DebugLZQ");
                int i = class1.GetSomthing("DebugLZQ");
                Console.WriteLine("输入的长度是,{0}", i);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
       
       
       
        }




    }
}











using System.Text;
using System.Xml;
using System.Windows.Forms;
using Atspace.DataInjection.AddFunctionLibrary;
using System.IO;
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingServer.Entry();
            RemotingClient.main();
            Console.ReadKey();
          
          
           }
       
        }
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值