您的位置:首页 > 其它

Webcast.NET Remoting学习笔记(1)第一个remoting程序

2007-02-02 17:24 495 查看
最近开始学习分布式程序设计,在网上下了msdn的视频教程,边看边学边记笔记,坚持把教程全部学习一遍。

先以一个简单的例子开始.NET Remoting的学习。
开发Remoting有三步:
1、创建远程对象
2、创建一个应用程序作为宿主,接受客户端的请求。
3、创建一个客户端调用远程对象。
下面开始第一步:
在解决方案中添加类库项目General

using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingSamples
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

namespace RemotingSamples
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;

namespace RemotingSamples
public class Client
public static void Main(string[] args)
//使用TCP通道得到远程对象
TcpChannel chan1 = new TcpChannel();
ChannelServices.RegisterChannel(chan1);
HelloServer obj1 = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"tcp://localhost:8085/SayHello");
if (obj1 == null)
System.Console.WriteLine(
"Could not locate TCP server");
}
//使用HTTP通道得到远程对象
HttpChannel chan2 = new HttpChannel();
ChannelServices.RegisterChannel(chan2);
HelloServer obj2 = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"http://localhost:8086/SayHello");
if (obj2 == null)
System.Console.WriteLine(
"Could not locate HTTP server");
}

Console.WriteLine(
"Client1 TCP HelloMethod {0}",
obj1.HelloMethod("服务器收到请回答1"));
Console.WriteLine(
"Client2 HTTP HelloMethod {0}",
obj2.HelloMethod("服务器收到请回答2"));
Console.ReadLine();
}

}
}然后开始执行(不调试)server,之后对client执行“启动新实例”可以看到结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: