博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A SIMPLE REMOTING EXAMPLE IN C#
阅读量:6113 次
发布时间:2019-06-21

本文共 4142 字,大约阅读时间需要 13 分钟。

After looking in vain for an easy example to understand the basics of remoting, I decided to write one myself. I found one or two useful articles, but they had syntax errors and left a lot for the reader to fill in. My example needs no tweaking and can be used as is. For simplicity, I use only one machine. The server and the client reside on the same machine.

Problem statement:

TicketServer holds information about the ticket status of a movie theater. A client needs to know the status of a ticket. Establish a connection between the client and the server so that client gets the information needed.

Solution:

A method called GetTicketStatus is defined in the server space. This method returns the status of the ticket. The server publishes this method which can be used by any client. The server listens to port 9998 over TCP. The client invokes the published method and gets the ticket status.

Implementation:

An interface MovieTicketInterface is defined which contains the GetMovieTicket method signature. This interface is implemented by MovieTicket class. The method GetMovieTicket is also implemented.

The server TicketServer registers the MovieTicket class as a remoting service. It listens to the port 9998 and waits for communication from any client.

The client creates an object of type MovieTicketInterface as a remoting object. As a part of this step, the communication between the server and the client over TCP on port number 9998 is established. It then invokes the method GetTicketStatus and gets the status.

Source code:

Server part:

1. Create a Console Application named TicketServer.

2. Add System.Runtime.Remoting as a reference to the project.
3. Replace the existing code in Class.cs with the following code and build the project.

 

01 using System;
02 using System.Runtime.Remoting;
03 using System.Runtime.Remoting.Channels;
04 using System.Runtime.Remoting.Channels.Tcp;
05  
06 class Program
07 {
08 static void Main(string[] args)
09 {
10 TicketServer();
11 }
12  
13 static void TicketServer()
14 {
15 Console.WriteLine("Ticket Server started...");
16  
17 TcpChannel tcpChannel = new TcpChannel(9998);
18 ChannelServices.RegisterChannel(tcpChannel);
19  
20 Type commonInterfaceType = Type.GetType("MovieTicket");
21  
22 RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
23 "MovieTicketBooking", WellKnownObjectMode.SingleCall);
24  
25 System.Console.WriteLine("Press ENTER to quitnn");
26 System.Console.ReadLine();
27  
28 }
29  
30 }
31  
32 public interface MovieTicketInterface
33 {
34 string GetTicketStatus(string stringToPrint);
35 }
36  
37 public class MovieTicket : MarshalByRefObject, MovieTicketInterface
38 {
39 public string GetTicketStatus(string stringToPrint)
40 {
41 string returnStatus = "Ticket Confirmed";
42 Console.WriteLine("Enquiry for {0}", stringToPrint);
43 Console.WriteLine("Sending back status: {0}", returnStatus);
44  
45 return returnStatus;
46 }
47 }

 

Client side:

1. Create a Console Application named Client.

2. Add System.Runtime.Remoting and Server.exe [See Note 1] as references to the project.
3. Replace the existing code in Class.cs with the following code and build the project.

 

01 using System;
02 using System.Runtime.Remoting;
03 using System.Runtime.Remoting.Channels;
04 using System.Runtime.Remoting.Channels.Tcp;
05  
06 class MyClient
07 {
08 public static void Main()
09 {
10 TcpChannel tcpChannel = new TcpChannel();
11 ChannelServices.RegisterChannel(tcpChannel);
12  
13 Type requiredType = typeof(MovieTicketInterface);
14  
15 MovieTicketInterface remoteObject = (MovieTicketInterface)Activator.GetObject(requiredType,
16 "tcp://localhost:9998/MovieTicketBooking");
17  
18 Console.WriteLine(remoteObject.GetTicketStatus("Ticket No: 3344"));
19 }
20 }

 

Execution:

1. Execute Server.exe

2. Execute Client.exe

You will see the appropriate messages on the client side and the remote side.

Note 1: If you are using Visual Studio 2003, you cannot add a reference to an exe file. Make a copy of Server.exe, rename it to Server.dll and this can be added as a reference in your client project.

 

转载于:https://www.cnblogs.com/zhangchenliang/archive/2013/02/22/2921735.html

你可能感兴趣的文章
设计模式(九)——桥接模式
查看>>
xen 创建本地存储
查看>>
TCP三次握手/四次挥手 | NAT介绍 |OSI与TCP/IP模型
查看>>
jQuery UI dialog 的使用
查看>>
ABP实战--集成Ladp/AD认证
查看>>
存储过程
查看>>
phpcms v9栏目列表调用每一篇文章内容方法
查看>>
python 自定义信号处理器
查看>>
我只是轻奢 40万内入门豪车最高让利7万!-搜狐汽车
查看>>
曲演杂坛--隐式转换
查看>>
远程桌面连接技巧--与主机拷贝文本及拷贝文件(转)
查看>>
MVC中下拉框显示枚举项
查看>>
Linux基础精华
查看>>
SqlServer2008第一次安装后连接问题
查看>>
cocos2d-x Schedule详解
查看>>
sdut 2163:Identifiers(第二届山东省省赛原题,水题)
查看>>
C++ 容器:顺序性容器、关联式容器和容器适配器
查看>>
mysql 常用语句集
查看>>
Atitit.软件开发提升稳定性总结
查看>>
lftp查看文件时间与登录服务查看文件时间相差8小时
查看>>