Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Getting Started with Remoting in .NET  : Page 4

Use remoting in .NET to let isolated processes communicate with each other and improve security and stability at the same time.

This demonstration program presents a simple remoting example. It illustrates the basic tasks involved in creating a remotable class, a server, and a client. To help keep things easy to understand, the demo makes use of .NET command line applications and the command-line compiler, but the same principles apply when you use remoting with other application types. To work with the demo, open a Visual Studio .NET (VS.NET) command prompt from the Start menu by selecting Microsoft Visual Studio .NET, then selecting Visual Studio .NET Tools, and finally Visual Studio .NET Command Prompt. The Command Prompt window opened by this command is like any other Windows command prompt but has the necessary environment and path variables set to let you use the VS.net command line compilers. Create a folder for the project, and make that folder current.

The remotable class (called RemoteClass) contains a single method that returns a "secret" word to the caller. The class constructor displays a console message when a client activates the class. I've included the console message for demonstration purposes only; it's not required for the class to function. Listing 1 shows the source code for the RemoteClass. Note that the only aspect of the class related to remoting is that it inherits from MarshalByRefObject. You can create the demo code using any text editor and then save it in the project folder under the name RemoteClass.cs. Then, from the command prompt, compile the class using the following command (enter the command on a single line):

csc /t:library /debug /r:System.Runtime.Remoting.dll RemoteClass.cs

The class compiles to the file RemoteClass.dll.

Next, you must build the client, which will call the remotable class. Listing 2 shows the client code, called Client.cs. The program does the following things:

  1. Creates a TcpChannel on port 8084.
  2. Registers the channel with the .NET infrastructure.
  3. Attempts to obtain a reference to the remote class by calling the Activator.GetObject() method.
  4. If the program can't obtain a reference, it displays a message to the user. Otherwise it calls the remote object's SecretWord() method and displays the returned data.
The call to the Activator.GetObject() method needs some more explanation. The method accepts two arguments. The first is the type of the remote class, which you can obtain by using the typeof() method with the class's namespace and name as argument. The second argument has the following parts:

  • tcp://: identifies the protocol being used. Tcp is specified because the client and the server are using a TcpChannel for communication.
  • localhost: the name of the computer on which the remote class is located. In this sample, the remote class is on the same computer as the client, so the code uses the name "localhost". If the class were located on a network computer with the name "BIGSERVER", you would use that instead.
  • :8085: This identifies the port on which the remote class is listening.
  • /Secret: the URI associated with the remote class. This is an arbitrary name and can be anything you like as long as it matches the URI established by the server.
To compile the client, save the code as Client.cs and compile with the following command line (enter the command on a single line). Note that the compiler command specifies a reference to the remotable class RemoteClass.dll that you created previously.

Given the power of remoting, the relative ease with which you can accomplished it comes as a welcome surprise to many developers.

csc /debug /r:remoteclass.dll /r:System.Runtime.Remoting.dll Client.cs

Finally, you need to create the server. The server listens for calls from clients and connects them to the remotable class. Listing 3 shows the server code. Here's what the code does:

  1. Creates a new TcpChannel on port 8085. Note that this is the same channel on which the client will look for the remotable class.
  2. Registers the channel with the .NET infrastructure.
  3. Registers the remotable class using a call to the RemotingConfiguration.RegisterWellKnownServiceType() method. The arguments to this call are:
  4. The first argument identifies the class being registered.
  5. The second argument specifies the URI for the class. A client will use this URI when looking for the class.
  6. The third argument specifies that if there are multiple calls to the class (from more than one client), they will all be services by the same instance of the class.
  7. Display a message to the user and then pause until the user presses Enter.
Step 4 is required because the server must be executing to do its job. In other words, only while the server program is running will it "listen" for client requests for the remotable class.

To complete the server, save the code from Listing 3 as Server.cs and compile it with the following command line (enter the command on a single line):

csc /debug /r:remoteclass.dll /r:System.Runtime.Remoting.dll Server.cs

Assuming all three parts of the project compiled successfully, you can test the program as follows.

  1. Open a second Command Prompt window and change to the project folder. You will now have two Command Prompt windows open (the need for this will become clear in a moment).
  2. In one of the windows enter "Server" to execute the server program. It will display the message "Hit <enter> to exit...". The server is now waiting, listening for client requests for the remotable class.
  3. In the other Command prompt window enter "Client" to execute the client program. You'll see two things happen:
  4. The client window displays the message "The secret word is REMOTING", and the client program terminates.
  5. The server window displays the message "MyRemoteClass activated", signaling that a client activated the remote class.
  6. To terminate the server, switch to the server window and press Enter.
You've seen a simple example introducing you to the .NET Framework's remoting capabilities. Given the power of remoting, the relative ease with which you can accomplished it comes as a welcome surprise to many developers. While few real-world scenarios are as simple as the demonstration program presented here, the principles remain the same.

Peter G. Aitken has been writing about computers and programming for over 10 years, with some 30 books and hundreds of articles to his credit. Recent book titles include Developing Office Solutions With Office 2000 Components and VBA, Windows Script Host, and the soon to be published XML the Microsoft Way. He is a regular contributor to OfficePro magazine, and for several years was a contributing editor for Visual Developer Magazine where he wrote the popular Visual Basic column. Peter is the proprietor of PGA Consulting, providing custom application and Internet development to business, academia, and government since 1994. You can reach him at peter@pgacon.com .
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date