A simple remoting example in C #

Source: Internet
Author: User

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 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.

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.