Csharp/C Sharp/Network/Http Channel — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:47, 26 мая 2010

HttpChannel Math Server

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class MathServer
{
   public static int Main()
   {
      HttpChannel chan = new HttpChannel(9050);
      ChannelServices.RegisterChannel(chan);
      RemotingConfiguration.RegisterWellKnownServiceType(
         Type.GetType("MathClass, MathClass"), "MyMathServer",
         WellKnownObjectMode.SingleCall);
      Console.WriteLine("Hit <enter> to exit...");
      Console.ReadLine();
      return 0;
    }
}

public class MathClass : MarshalByRefObject
{
   public int Add(int a, int b)
   {
     int c = a + b;
     return c;
   }
   public int Subtract(int a, int b)
   {
      int c = a - b;
      return c;
   }
   public int Multiply(int a, int b)
   {
      int c = a * b;
      return c;
   }
   public int Divide(int a, int b)
   {
      int c;
      if (b != 0)
         c = a / b;
      else
         c = 0;
      return c;
   }
}


Math Client

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class MathClient
{
   public static int Main(string[] argv)
   {
      HttpChannel chan = new HttpChannel();
      ChannelServices.RegisterChannel(chan);
      MathClass obj = (MathClass)Activator.GetObject(
          typeof(MathClass), "http://127.0.0.1:9050/MyMathServer");
      if (obj == null) 
         System.Console.WriteLine("Could not locate server");
      else
      {
         int a = Convert.ToInt32(argv[0]);
         int b = Convert.ToInt32(argv[1]);
         int c = obj.Add(a, b);
         Console.WriteLine("a + b = {0}", c);
         c = obj.Subtract(a, b);
         Console.WriteLine("a - b = {0}", c);
         c = obj.Multiply(a, b);
         Console.WriteLine("a * b = {0}", c);
         c = obj.Divide(a, b);
         Console.WriteLine("a / b = {0}", c);
      }
      return 0;
    } 
}

public class MathClass : MarshalByRefObject
{
   public int Add(int a, int b)
   {
     int c = a + b;
     return c;
   }
   public int Subtract(int a, int b)
   {
      int c = a - b;
      return c;
   }
   public int Multiply(int a, int b)
   {
      int c = a * b;
      return c;
   }
   public int Divide(int a, int b)
   {
      int c;
      if (b != 0)
         c = a / b;
      else
         c = 0;
      return c;
   }
}


New Math Client

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class NewMathClient
{
   public static int Main(string[] argv)
   {
      HttpChannel chan = new HttpChannel();
      ChannelServices.RegisterChannel(chan);
      MathClass obj = new MathClass();
      if (obj == null) 
         System.Console.WriteLine("Could not locate server");
      else
      {
         int a = Convert.ToInt32(argv[0]);
         int b = Convert.ToInt32(argv[1]);
         int c = obj.Add(a, b);
         Console.WriteLine("a + b = {0}", c);
         c = obj.Subtract(a, b);
         Console.WriteLine("a - b = {0}", c);
         c = obj.Multiply(a, b);
         Console.WriteLine("a * b = {0}", c);
         c = obj.Divide(a, b);
         Console.WriteLine("a / b = {0}", c);
      }
      return 0;
    } 
}

public class MathClass : MarshalByRefObject
{
   public int Add(int a, int b)
   {
     int c = a + b;
     return c;
   }
   public int Subtract(int a, int b)
   {
      int c = a - b;
      return c;
   }
   public int Multiply(int a, int b)
   {
      int c = a * b;
      return c;
   }
   public int Divide(int a, int b)
   {
      int c;
      if (b != 0)
         c = a / b;
      else
         c = 0;
      return c;
   }
}