Csharp/C Sharp/Network/Http Channel

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

HttpChannel Math Server

<source lang="csharp"> 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;
  }

}

      </source>


Math Client

<source lang="csharp"> 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;
  }

}

      </source>


New Math Client

<source lang="csharp"> 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;
  }

}


      </source>