简单Remoting例子(1)

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

31InBlock.gif
32ExpandedSubBlockEnd.gif    }

33ExpandedBlockEnd.gif}
General:
ContractedBlock.gif ExpandedBlockStart.gif 提供服务的真实类
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace RemotingSamples
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class HelloServer : MarshalByRefObject
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public HelloServer()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            Console.WriteLine("HelloServer activated");
12ExpandedSubBlockEnd.gif        }

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

19ExpandedSubBlockEnd.gif    }

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

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

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

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

49ExpandedSubBlockEnd.gif    }

50ExpandedBlockEnd.gif}

51 None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值