remoting例子

///服务端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataInterface
{
    /// <summary>
    /// 运算接口
    /// </summary>
    public interface Icalculate
    {
        /// <summary>
        /// 加法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        double Add(double x,double y);
        /// <summary>
        /// 减法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        double Subtract(double x, double y);
        /// <summary>
        /// 乘法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        double Multiply(double x, double y);
        /// <summary>
        /// 除法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        double Division(double x, double y);
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataInterface;

namespace AccessData
{
    /// <summary>
    /// 实现运算接口
    /// </summary>
    public class calculate :Icalculate
    {
        #region Icalculate 成员
        public double Add(double x, double y)
        {
            return x + y;
        }
        public double Subtract(double x, double y)
        {
            return x - y;
        }
        public double Multiply(double x, double y)
        {
            return x * y;
        }
        public double Division(double x, double y)
        {
            return x / y;
        }
        #endregion
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataInterface;

namespace AccessData
{
    /// <summary>
    /// 抛出边界访问对象 要继承MarshalByRefObject
    /// </summary>
    public class calculateRemotingProxy:MarshalByRefObject,Icalculate
    {
        Icalculate calculateremotingproxy;
        public calculateRemotingProxy()
        {
            calculateremotingproxy = new calculate();
        }
        #region Icalculate 成员

        public double Add(double x, double y)
        {
            return calculateremotingproxy.Add(x, y);
        }

        public double Subtract(double x, double y)
        {
            return calculateremotingproxy.Subtract(x, y);
        }

        public double Multiply(double x, double y)
        {
            return calculateremotingproxy.Multiply(x, y);
        }

        public double Division(double x, double y)
        {
            return calculateremotingproxy.Division(x, y);
        }

        #endregion
    }
}

 

 

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

namespace remotingserver
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("服务已启动!");
            TcpServerChannel serverchannel = new TcpServerChannel(8811);//服务器信道
            ChannelServices.RegisterChannel(serverchannel);//注册信道
            //此处要注册的实现类而非接口 注册公布对象
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(calculateRemotingProxy), "calculate", WellKnownObjectMode.SingleCall);
            Console.ReadLine();
        }
    }
}

 

///客户端 要引用接口

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

namespace remotingclient
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClientChannel clientchannel = new TcpClientChannel();//客户端通道
            ChannelServices.RegisterChannel(clientchannel);//注册通道
            //此处获得接口
            Icalculate obj1 =(Icalculate) Activator.GetObject(typeof(Icalculate), "tcp://localhost:8811/calculate");
            double result = obj1.Add(11.0, 34.0);
            Console.WriteLine(result.ToString());
            Console.ReadLine();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值