1,远程对象,必须继承MarshalByRefObject类
using System;
using System.Collections.Generic;
using System.Text;
namespace RemotingSamples
{
//远程对象类型
public class HelloServer : MarshalByRefObject
{
public HelloServer()
{
Console.WriteLine("HelloServer activated");
}
public String HelloMethod(String name)
{
Console.WriteLine("Server Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
}
}
2,服务器端
using System.Collections.Generic;
using System.Text;
namespace RemotingSamples
{
//远程对象类型
public class HelloServer : MarshalByRefObject
{
public HelloServer()
{
Console.WriteLine("HelloServer activated");
}
public String HelloMethod(String name)
{
Console.WriteLine("Server Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
}
}
using System;
//命名空间导入
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace RemotingSamples
{
//服务器类
public class Server
{
public static int Main(string [] args)
{
//tcp通道
TcpChannel chan1 = new TcpChannel(8085);
//http通道
HttpChannel chan2 = new HttpChannel(8086);
//注册通道
ChannelServices.RegisterChannel(chan1);
ChannelServices.RegisterChannel(chan2);
//服务器端激活
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(HelloServer), //远程对象类型
"SayHello", //url
WellKnownObjectMode.Singleton //唯一实例
);
System.Console.WriteLine("Press Enter key to exit");
System.Console.ReadLine();
return 0;
}
}
}
3,客户端方法
//命名空间导入
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace RemotingSamples
{
//服务器类
public class Server
{
public static int Main(string [] args)
{
//tcp通道
TcpChannel chan1 = new TcpChannel(8085);
//http通道
HttpChannel chan2 = new HttpChannel(8086);
//注册通道
ChannelServices.RegisterChannel(chan1);
ChannelServices.RegisterChannel(chan2);
//服务器端激活
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(HelloServer), //远程对象类型
"SayHello", //url
WellKnownObjectMode.Singleton //唯一实例
);
System.Console.WriteLine("Press Enter key to exit");
System.Console.ReadLine();
return 0;
}
}
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
namespace RemotingSamples
{
public class Client
{
public static void Main(string[] args)
{
//使用TCP通道得到远程对象
TcpChannel chan1 = new TcpChannel();
ChannelServices.RegisterChannel(chan1);
HelloServer obj1 = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"tcp://localhost:8085/SayHello");
if (obj1 == null)
{
System.Console.WriteLine(
"Could not locate TCP server");
}
//使用HTTP通道得到远程对象
HttpChannel chan2 = new HttpChannel();
ChannelServices.RegisterChannel(chan2);
HelloServer obj2 = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"http://localhost:8086/SayHello");
if (obj2 == null)
{
System.Console.WriteLine(
"Could not locate HTTP server");
}
//调用远程方法
Console.WriteLine(
"Client1 TCP HelloMethod {0}",
obj1.HelloMethod("Caveman1"));
Console.WriteLine(
"Client2 HTTP HelloMethod {0}",
obj2.HelloMethod("Caveman2"));
Console.ReadLine();
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
namespace RemotingSamples
{
public class Client
{
public static void Main(string[] args)
{
//使用TCP通道得到远程对象
TcpChannel chan1 = new TcpChannel();
ChannelServices.RegisterChannel(chan1);
HelloServer obj1 = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"tcp://localhost:8085/SayHello");
if (obj1 == null)
{
System.Console.WriteLine(
"Could not locate TCP server");
}
//使用HTTP通道得到远程对象
HttpChannel chan2 = new HttpChannel();
ChannelServices.RegisterChannel(chan2);
HelloServer obj2 = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"http://localhost:8086/SayHello");
if (obj2 == null)
{
System.Console.WriteLine(
"Could not locate HTTP server");
}
//调用远程方法
Console.WriteLine(
"Client1 TCP HelloMethod {0}",
obj1.HelloMethod("Caveman1"));
Console.WriteLine(
"Client2 HTTP HelloMethod {0}",
obj2.HelloMethod("Caveman2"));
Console.ReadLine();
}
}
}
浙公网安备 33010602011771号