///服务端
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();
}
}
}