September 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

.NET Remoting

  • October 10, 2002
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Create a Client To Use the Remotable Object

Now that we have our remotable object and a server object to listen for requests, let's create a client to use it. Our client will be very simple. It will connect to the server, create an instance of the object using the server, and then execute the HelloWorld method.

Create a new C# console application project. Add a class called SampleClient and paste in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. In addition, add a reference to the project containing the SampleObject, otherwise the code will not compile because it won't know how to find a reference to SampleObject. Compile the class to make sure you have everything correct.

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;namespace CodeGuru.Remoting{  /// <remarks>  /// Sample client to demonstrate the use of .NET Remoting.  /// </remarks>  public class SampleClient  {   public static int Main(string [] args)   {     // Create a channel for communicating w/ the remote object     // Notice no port is specified on the client     TcpChannel chan = new TcpChannel();     ChannelServices.RegisterChannel(chan);     // Create an instance of the remote object     SampleObject obj = (SampleObject) Activator.GetObject(          typeof(CodeGuru.Remoting.SampleObject),         "tcp://localhost:8080/HelloWorld" );     // Use the object     if( obj.Equals(null) )     {       System.Console.WriteLine("Error: unable to locate server");     }     else     {       Console.WriteLine(obj.HelloWorld());     }     return 0;   }   }}

Test the Remoting Sample

Once you have created the projects and successfully compiled each of them you are ready to try it out. Assuming you chose a free TCP/IP port for the service, start the server executable. After the server successfully starts it will result in a console window being displayed with the message "Press the enter key to exit". The server is listening so you are now ready to run the client. Executing the client should result in "Hello World!" being displayed in a separate console window. The client window will then close while the server remains open and available.

If you have multiple computers available to you on a network you could execute the server on one machine and the client on another just to prove to yourself that it really is remoting. In order to run on separate machines you would need to change the reference to localhost in the sample client to point to the appropriate location.

Summary

.NET Remoting is a powerful way to enable interprocess communication. It is more complicated to program against than Web services. You need to decide for yourself whether your standard architecture is to use .NET Remoting or Web services.

Future Columns

The next column will be on the use of encryption in the .NET framework. We'll take a look at some of the encryption algorithms available in the framework and ways to use them. If you have a particular topic in mind please email me at mstrawmyer@crowechizek.com

About the Author

Mark Strawmyer, MCSD, MCSE (NT4/W2K), MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@crowechizek.com.





Page 3 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel