简单Remoting例子(1)

 

服务器:

 1using System.Runtime.Remoting.Channels.Tcp;
 2using System.Runtime.Remoting.Channels.Http;
 3
 4namespace RemotingSamples 
 5{
 6
 7    public class Server
 8    {
 9        public static int Main(string [] args) 
10        {
11
12            TcpChannel chan1 = new TcpChannel(8085);
13            HttpChannel chan2 = new HttpChannel(8086);
14
15            ChannelServices.RegisterChannel(chan1);
16            ChannelServices.RegisterChannel(chan2);
17
18
19            RemotingConfiguration.RegisterWellKnownServiceType
20                (
21                typeof(HelloServer),
22                "SayHello",
23                WellKnownObjectMode.Singleton
24                );
25            
26
27            System.Console.WriteLine("Press Enter key to exit");
28            System.Console.ReadLine();
29            return 0;
30        }

31
32    }

33}
General:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace RemotingSamples
 6{
 7    public class HelloServer : MarshalByRefObject
 8    {
 9        public HelloServer()
10        {
11            Console.WriteLine("HelloServer activated");
12        }

13        public String HelloMethod(String name)
14        {
15            Console.WriteLine(
16                "Server Hello.HelloMethod : {0}", name);
17            return "Hi there " + name;
18        }

19    }

20}
客户:
 1 using  System;
 2 using  System.Runtime.Remoting;
 3 using  System.Runtime.Remoting.Channels;
 4 using  System.Runtime.Remoting.Channels.Tcp;
 5 using  System.Runtime.Remoting.Channels.Http;
 6 using  System.IO;
 7
 8 namespace  RemotingSamples 
 9 {
10    public class Client
11    {
12        public static void Main(string[] args)
13        {
14            //使用TCP通道得到远程对象
15            TcpChannel chan1 = new TcpChannel();
16            ChannelServices.RegisterChannel(chan1);
17            HelloServer obj1 = (HelloServer)Activator.GetObject(
18                typeof(RemotingSamples.HelloServer),
19                "tcp://localhost:8085/SayHello");
20            if (obj1 == null)
21            {
22                System.Console.WriteLine(
23                    "Could not locate TCP server");
24            }

25            //使用HTTP通道得到远程对象
26            HttpChannel chan2 = new HttpChannel();
27            ChannelServices.RegisterChannel(chan2);
28            HelloServer obj2 = (HelloServer)Activator.GetObject(
29                typeof(RemotingSamples.HelloServer),
30                "http://localhost:8086/SayHello");
31            if (obj2 == null)
32            {
33                System.Console.WriteLine(
34                    "Could not locate HTTP server");
35            }

36
37            Console.WriteLine(
38                "Client1 TCP HelloMethod {0}",
39                obj1.HelloMethod("Caveman1"));
40            Console.WriteLine(
41                "Client2 HTTP HelloMethod {0}",
42                obj2.HelloMethod("Caveman2"));
43            Console.ReadLine();
44        }

45/*
46            RemotingConfiguration.Configure(@"C:/Documents and Settings/RenMin/桌面/Remoting/Demo/02-SimpleRemoting/Client/client.exe.config");
47            HelloServer obj2 = new HelloServer();
48*/

49    }

50}

 基础篇: http://www.cnblogs.com/wayfarer/archive/2004/07/30/28723.html

很好的 一个学习贴:

Web Service , Remoting 我该用哪个?

 

--------------------------------------------------------


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值