// ===================================== object.cs // Obiekty rozproszone, przyklad deklaracji typow // obiektow udostepnianych zdalnie // 14 stycznia 2003, Marcin Mlotkowski using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; namespace RemotingSamples { // tylko udostepnia zdalne uslugi public class Matematyka : MarshalByRefObject { public Matematyka() { Console.WriteLine("Serwer Matematyka uruchomiony"); } public int fib(int arg) { int a3, a2, a1; int n; a3 = (a2 = (a1 = 1)); n = 2; while (n <= arg) { a3 = a2 + a1; a1 = a2; a2 = a3; n = n + 1; } return a3; } } // udostepnia zdalne uslugi, moze byc tez zdalnie przeslany stan // [Serializable] public class Argument : MarshalByRefObject { int wywolanie; // stan obiektu public Argument() { Console.WriteLine("Obiekt Argument utworzony"); wywolanie = 0; } public int Inc () { wywolanie = wywolanie + 1; return wywolanie; } public Argument self() { return this; } public void Hello() { Console.WriteLine("Tu obiekt Argument"); } } } // ======================================= server.cs // Obiekty rozproszone, rejestracja obiektow // udostepniajacych uslugi // 14 stycznia 2003, Marcin Mlotkowski using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; namespace RemotingSamples { public class Sample { public static int Main(string [] args) { HttpChannel chan = new HttpChannel(8085); ChannelServices.RegisterChannel(chan); RemotingConfiguration.RegisterWellKnownServiceType (Type.GetType("RemotingSamples.Matematyka, object"), "Matma", WellKnownObjectMode.SingleCall); RemotingConfiguration.RegisterWellKnownServiceType (Type.GetType("RemotingSamples.Argument, object"), "Zdalne", WellKnownObjectMode.Singleton); System.Console.WriteLine("Hit to exit..."); System.Console.ReadLine(); return 0; } } } // ===================================== client.cs // Obiekty rozproszone, plikacja klienta // 14 stycznia 2003, Marcin Mlotkowski using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; // Moze tez byc Tcp zamiast Http namespace RemotingSamples { public class Client { public static int Main(string [] args) { HttpChannel chan = new HttpChannel(); ChannelServices.RegisterChannel(chan); // test uslugi Matematyka Matematyka mat = (Matematyka)Activator.GetObject(typeof(RemotingSamples.Matematyka), "http://localhost:8085/Matma"); if (mat == null) System.Console.WriteLine("nie ma serwera Matematyka"); else { Console.WriteLine("fib = " + mat.fib(5)); Console.WriteLine("fib = " + mat.fib(12)); } // Test przesylania obiektu Argument arg = (Argument)Activator.GetObject(typeof(RemotingSamples.Argument), "http://localhost:8085/Zdalne"); if (arg == null) System.Console.WriteLine("nie ma serwera Zdalne"); else { // Console.WriteLine("Zdalnie: " + arg.Inc()); // Console.WriteLine("Zdalnie: " + arg.Inc()); Argument loc = arg.self(); // lokalna kopia obiektu Console.WriteLine("Lokalnie: " + loc.Inc()); Console.WriteLine("Lokalnie: " + loc.Inc()); Console.WriteLine("Zdalnie: " + arg.Inc()); } return 0; } } } // ================================== compile.bat REM kompilacja uslug @echo off cls csc /debug+ /target:library /out:object.dll object.cs csc /debug+ /r:object.dll /r:System.Runtime.Remoting.dll server.cs csc /debug+ /r:object.dll /r:server.exe /r:System.Runtime.Remoting.dll client.cs REM wyciagniecie sygnatury uslug udostepnianych przez Matma REM serwer musi byc uruchomiony soapsuds -url:http://localhost:8085/Matma?wsdl /os:plik.xml /oa:plik.dll