13.07.2015 Views

Remoting in C# and .NET - The Journal of Object Technology

Remoting in C# and .NET - The Journal of Object Technology

Remoting in C# and .NET - The Journal of Object Technology

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

REMOTING IN <strong>C#</strong> AND .<strong>NET</strong>Let us consider a simple client server application. <strong>The</strong> server uses a support<strong>in</strong>gNameHolder class that encapsulates an ArrayList <strong>of</strong> Str<strong>in</strong>g objects, names, each hold<strong>in</strong>g aperson’s name. <strong>The</strong> server has an AddName method that uses a Str<strong>in</strong>g parameter to addanother object to the names field with<strong>in</strong> the NameHolder object.<strong>The</strong> client application takes Console <strong>in</strong>put as it is <strong>in</strong>voked <strong>and</strong> passes the str<strong>in</strong>grepresent<strong>in</strong>g a person’s name to the server object serv<strong>in</strong>g as a proxy. It will be clear fromthe code how this is all accomplished. Let us exam<strong>in</strong>e the details <strong>of</strong> List<strong>in</strong>g 1.List<strong>in</strong>g 1 – Simple Client/Server application us<strong>in</strong>g <strong>Remot<strong>in</strong>g</strong>us<strong>in</strong>g System;us<strong>in</strong>g System.Runtime.<strong>Remot<strong>in</strong>g</strong>;us<strong>in</strong>g System.Runtime.<strong>Remot<strong>in</strong>g</strong>.Channels;us<strong>in</strong>g System.Collections;namespace <strong>Remot<strong>in</strong>g</strong> {// Server classpublic class SaveNamesServer : MarshalByRef<strong>Object</strong> {// Fieldsprivate NameHolder holder; // Must be serializable// Constructorpublic SaveNamesServer() {holder = new NameHolder();}// Comm<strong>and</strong>spublic void AddName(Str<strong>in</strong>g name) {holder.AddName(name);}}}// Queriespublic ArrayList GetNames() {return holder.GetNames();}us<strong>in</strong>g System;us<strong>in</strong>g System.Collections;namespace <strong>Remot<strong>in</strong>g</strong> {[Serializable]public class NameHolder {// Fieldsprivate ArrayList names = new ArrayList();VOL. 3, NO. 1 JOURNAL OF OBJECT TECHNOLOGY 85

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!