自己动手写Remoting(三)
来源:互联网 发布:怎么把不安全网络ipad 编辑:程序博客网 时间:2024/05/19 23:24
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace Client
{
class Client
{
static void Main(string[] Args)
{
#region TCP
TcpChannel tcpChannel = new TcpChannel(); //因为客户端不需要CallBack端口
ChannelServices.RegisterChannel(tcpChannel, false);
#endregion
HttpClientChannel httpChannel = new HttpClientChannel();
ChannelServices.RegisterChannel(httpChannel, false);
//取得远程的Remoting对象
ShareDLL.InterMyService Service = (ShareDLL.InterMyService)Activator.GetObject(typeof(ShareDLL.InterMyService),
"tcp://10.0.1.52:9000/ServiceTestTCP");
ShareDLL.ICAOObject obj1 = Service.CreateCAO("Brian"); //在客户端映射创建一个对象
Console.WriteLine(obj1.SayHelloCAO()); //可以直接使用这个对象
ShareDLL.InterMyService ServiceHTTP = (ShareDLL.InterMyService)Activator.GetObject(typeof(ShareDLL.InterMyService),
"http://10.0.1.52:9012/ServiceTestHTTP");
ShareDLL.ICAOObject obj2 = ServiceHTTP.CreateCAO("KaiWei");
Console.WriteLine(obj2.SayHelloCAO());
Console.WriteLine(ServiceHTTP.SayHello("KaiWei"));
Console.ReadLine();
}
}
}
以上是client改写成Http通道
中间层不变同上
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace Server
{
class Server
{
static void Main(string[] args)
{
#region 走TCP通道
TcpChannel tcpChannel = new TcpChannel(9000);
//把tcpChannel对象注册到ChannelServices中
ChannelServices.RegisterChannel(tcpChannel, false);
//注册一个Remoting对象ServiceImpl(对象类型,对象名称,创建类型)
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceImpl), "ServiceTestTCP", WellKnownObjectMode.Singleton);
#endregion
HttpServerChannel httpChannel = new HttpServerChannel(9012);
ChannelServices.RegisterChannel(httpChannel, false);
RemotingConfiguration.ApplicationName = "Server";
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceImpl), "ServiceTestHTTP", WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterActivatedServiceType(typeof(ShareDLL.InterMyService));
Console.WriteLine("Server is Running");
Console.ReadLine();
Console.ReadLine();
}
}
//建立一个继承MarshalByRefObject和自己定义接口的Class
public class ServiceImpl : MarshalByRefObject, ShareDLL.InterMyService
{
public string SayHello(string AName)
{
return string.Format("Hello {0}", AName);
}
public ShareDLL.ICAOObject CreateCAO(string AName)
{
return new ICAOImpl(AName);
}
}
//(Client Activate Object) CAO 创建一个客户端激活的对象类
public class ICAOImpl : MarshalByRefObject, ShareDLL.ICAOObject
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public string SayHelloCAO()
{
return string.Format("Hello {0}, I'm CAO ", _Name);
}
public ICAOImpl(string AName)
{
_Name = AName;
}
}
}
以上remoting 内容改写为走HTTP通道!
- 自己动手写Remoting(三)
- 自己动手写俄罗斯方块(三)
- 自己动手写操作系统(三)
- 自己动手写操作系统(三)
- 自己动手写操作系统(三)
- 自己动手写俄罗斯方块(三)
- 自己动手写操作系统(三)
- 自己动手学Remoting(一)
- 自己动手学Remoting(二)
- 自己动手写操作系统(三) 作者:伊梅
- 自己动手写操作系统(三) 作者:伊梅
- 自己动手写basic解释器(三)
- 自己动手写操作系统-启动顺序之前三
- 自己动手写word2vec (三):构建Huffman树
- 自己动手写一个简单的Shell之三:命令行解析
- 自己动手写C语言格式化输出函数(三)
- 自己动手写字符串库函数 三(C语言实现)
- 自己动手写spring(三) 支持注解方式
- s3c2410 中断异常处理(转)
- weblogic9 开发 web services
- 参考文献格式规范
- MSNP10协议分析 02.同步 [by progsoft]
- 平淡生活:淡淡的雪
- 自己动手写Remoting(三)
- MSNP10协议分析 03.状态 [by progsoft]
- Visual C++编译器常用选项设置1
- MSNP10协议分析 04.Session [by progsoft]
- 通过js ,给页面的隐藏值赋值
- MSNP10协议分析 05.文本信息 [by progsoft]
- MSNP10协议分析 06.帐户验证 [by progsoft]
- EDB Linux Debugger 0.8.22 by proxy
- .net 中的file->new->webSite与create->webSite的区别