zoukankan      html  css  js  c++  java
  • 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.

    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,
    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/

  • 相关阅读:
    windows7 gvim 配置(好用)
    程序员的惰性
    java性能提升
    spring cloud gateway 负载均衡流程
    广告业务
    mysql二进制binarylog监控服务
    虽然垃圾,以后用得到在翻一翻,优化下
    hash冲突后处理
    jdk1.6乃至1.7中的Iepoll空轮询bug,netty中是怎样解决的
    树形结构
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2921735.html
Copyright © 2011-2022 走看看