A SIMPLE REMOTING EXAMPLE IN C#

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.

 

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

 

01using System;
02using System.Runtime.Remoting;
03using System.Runtime.Remoting.Channels;
04using System.Runtime.Remoting.Channels.Tcp;
05 
06class MyClient
07{
08public static void Main()
09{
10TcpChannel tcpChannel = new TcpChannel();
11ChannelServices.RegisterChannel(tcpChannel);
12 
13Type requiredType = typeof(MovieTicketInterface);
14 
15MovieTicketInterface remoteObject = (MovieTicketInterface)Activator.GetObject(requiredType,
16"tcp://localhost:9998/MovieTicketBooking");
17 
18Console.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.

 

http://generally.wordpress.com/2007/05/31/a-simple-remoting-example-in-c/

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值