Remoting Demo
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Runtime.Remoting;
12
using System.Runtime.Remoting.Channels;
13
using System.Runtime.Remoting.Channels.Tcp;
14
using System.Threading;
15![]()
16
public partial class remoting : System.Web.UI.Page
17
{
18
protected void Page_Load(object sender, EventArgs e)
19
{
20
Server1();
21
Client();
22
}
23
public class test : MarshalByRefObject
24
{
25
public string doing()
26
{
27
string date = DateTime.Now.ToString();
28
Thread.Sleep(5000);
29
return string.Format("{0} in {1}", this.GetType().Name,date+ AppDomain.CurrentDomain.FriendlyName+DateTime.Now.ToString());
30
}
31
}
32
public void Server1()
33
34
AppDomain server = AppDomain.CreateDomain("server");
35
IChannel[] channels = ChannelServices.RegisteredChannels;
36![]()
37
//关闭指定名为MyTcp的通道;
38
foreach (IChannel eachChannel in channels)
39
{
40
if (eachChannel.ChannelName == "tcp")
41
{
42
TcpServerChannel tcpChannel = (TcpServerChannel)eachChannel;
43![]()
44
//关闭监听;
45
tcpChannel.StopListening(null);
46![]()
47
//注销通道;
48
ChannelServices.UnregisterChannel(tcpChannel);
49
}
50
}
51![]()
52
TcpServerChannel tcpsc = new TcpServerChannel(1234);
53
ChannelServices.RegisterChannel(tcpsc, false);
54![]()
55
RemotingConfiguration.RegisterWellKnownServiceType(typeof(test), "test", WellKnownObjectMode.Singleton);
56
57
}
58![]()
59
public void Client()
60
{
61
IChannel[] channels = ChannelServices.RegisteredChannels;
62
foreach (IChannel eachChannel in channels)
63
{
64
if (eachChannel.ChannelName == "TcpChannel")
65
{
66
TcpChannel tcpChannel = (TcpChannel)eachChannel;
67![]()
68
//关闭监听;
69
tcpChannel.StopListening(null);
70![]()
71
//注销通道;
72
ChannelServices.UnregisterChannel(tcpChannel);
73
}
74
}
75
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
76
provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
77![]()
78
Hashtable props = new Hashtable();
79![]()
80
props["port"] = 7654;
81
props["name"] = "TcpChannel";
82![]()
83![]()
84
TcpChannel Cchannel = new TcpChannel(props, null, provider);
85![]()
86
// TcpClientChannel Cchannel= new TcpClientChannel();
87
// Cchannel.ChannelName="Cchannel";
88
ChannelServices.RegisterChannel(Cchannel, false);
89
test remo = (test)Activator.GetObject(typeof(test), "tcp://localhost:1234/test");
90
Response.Write(DateTime.Now);
91
Response.Write( remo.doing());
92
Response.Write(DateTime.Now);
93
}
94![]()
95
}
96

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96
