Remoting 的使用

一 远程对象

  /*
     *  用于远程调用的类,此类必须继承于 MarshalByRefObject
     
*/

    
public   class  Hello : MarshalByRefObject
    
{
        
private int count = 0;
        
public string SayHello(string name)
        
{
            
return "Hello, " + name + "!";
        }

        
public int GetCount()
        
{
            
return ++count;
        }

    }

二  Server端

 

class  Server
    
{
        
static void Main(string[] args)
        
{
            
// 使用 Server Activation 方式
            
//HelloServerActivated();
            
// 使用 Client Activation 方式
            HelloClientActivated();
            
//EmployeeClientActivated();
            Console.Read();
        }

        
// class Hello Server Activation 方式,有 SingleCall 和 Singleton 两种方式
        public static void HelloServerActivated()
        
{
            
try
            
{
                
// 使用 TCP 通道
                TcpChannel tcp = new TcpChannel(9999);
                ChannelServices.RegisterChannel(tcp, 
false);
               
               
                
// 使用 IPC 通道
                
//IpcChannel ipc = new IpcChannel("demo");
                
//ChannelServices.RegisterChannel(ipc, false);
                
// 使用 HTTP 通道
                
//HttpChannel http = new HttpChannel(10000);
                
//ChannelServices.RegisterChannel(http, false);
                RemotingConfiguration.RegisterWellKnownServiceType(
                    
typeof(RemoteObject.Hello), "Hello", WellKnownObjectMode.SingleCall);
                Console.WriteLine(
"Hello class is registered on server-side...");
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.ToString());
            }

        }

        
// class Hello Client Activation 方式
        public static void HelloClientActivated()
        
{
            
try
            
{
                
// 使用 TCP 通道
                TcpChannel tcp = new TcpChannel(9999);
                ChannelServices.RegisterChannel(tcp, 
false);
               
                RemotingConfiguration.RegisterActivatedServiceType(
typeof(RemoteObject.Hello));
                Console.WriteLine(
"Hello class is registered on server-side...");
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.ToString());
            }

        }

        
// class Employee Client Activation 方式
        public static void EmployeeClientActivated()
        
{
            
try
            
{
                
// 使用 TCP 通道
                TcpChannel tcp = new TcpChannel(9999);
                ChannelServices.RegisterChannel(tcp, 
false);
                RemotingConfiguration.RegisterActivatedServiceType(
typeof(RemoteObject.Employee));
                Console.WriteLine(
"Employee class is registered on server-side...");
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.ToString());
            }

        }

    }

三 客户端

 

class  Client
    
{
        
static void Main(string[] args)
        
{
            
// 使用 Server Activation 方式
            
//HelloServerActivated();
            
// 使用 Client Activation 方式
            HelloClientActivated();
            
//EmployeeClientActivated(args);
            Console.Read();
        }

        
// class Hello Server Activation 方式
        public static void HelloServerActivated()
        
{
            
try
            
{
                
// 使用 TCP 通道
                Hello hello = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:9999/Hello");
                
// 使用 IPC 通道
                
//Hello hello = (Hello)Activator.GetObject(typeof(Hello), "ipc://demo/Hello");
                
// 使用 HTTP 通道
                
//Hello hello = (Hello)Activator.GetObject(typeof(Hello), "http://localhost:10000/Hello");

                
if (hello == null)
                    Console.WriteLine(
"could not locate server.");
                
else
                
{
                    Console.WriteLine(hello.SayHello(
"Michael Jordan"));
                    Console.WriteLine(hello.GetCount());
                    Console.WriteLine(hello.GetCount());
                    Console.WriteLine(hello.GetCount());
                }
               
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.ToString());
            }

        }

        
// class Hello Client Activation 方式
        public static void HelloClientActivated()
        
{
            
try
            
{
                
object[] url = new UrlAttribute("tcp://localhost:9999") };
                Hello hello 
= (Hello)Activator.CreateInstance(typeof(Hello), null, url);
                
// 第二个参数是传入的构造函数的参数,null指不传入参数
 
                
if (hello == null)
                    Console.WriteLine(
"could not locate server.");
                
else
                
{
                    Console.WriteLine(hello.SayHello(
"Michael Jordan"));
                    Console.WriteLine(hello.GetCount());
                    Console.WriteLine(hello.GetCount());
                    Console.WriteLine(hello.GetCount());
                }

            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.ToString());
            }

        }

        
// class Employee Client Activation 方式
        public static void EmployeeClientActivated(string[] input)
        
{
            
try
            
{            
                
object[] args = input;
                
object[] url = new UrlAttribute("tcp://localhost:9999") };
                Employee emp 
= (Employee)Activator.CreateInstance(typeof(Employee), args, url);
               
                
if (emp == null)
                    Console.WriteLine(
"could not locate server.");
                
else
                
{
                    Console.WriteLine(
"Your name is " + emp.GetName());
                }

            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.ToString());
            }

 
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值