远程通迅

yaohangnet 2003-08-16 12:35:17
Remoting进行远程通迅 给点例子
...全文
23 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
大户翁 2003-08-16
  • 打赏
  • 举报
回复
1.建立一个类库项目:P1
a.类继承于System.MarshalByRefObject
b.定义要公有访问的类A1
2.建立一个服务器项目
a.增加System.Runtime.Remototing和类库项目的引用
b.增加引用:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.启动服务
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);//注册服务器通道
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);//注册远程服务对象类型
System.Console.WriteLine("hit to exit");
System.Console.ReadLine(); //等待
3.建立一个客户端项目
a.增加System.Runtime.Remototing和类库项目的引用
b.增加引用:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.调用服务
ChannelServices.RegisterChannel(new TcpClientChannel());//注册客户通道
Hello obj=(Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");//使用透明代理与服务器通信
if(obj==null)//检查通信是否成功
{
Console.WriteLine("Can not locate server!");
return;
}
for(int i=0;i<5;i++)//调用服务器上提供的函数
{
Console.WriteLine(obj.Greeting("glf"));
}
Console.WriteLine("hit to exit");
Console.ReadLine();

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧