草原上的野狼

啸苍天,没日月,孤单影只; 忍地寒,耐绝境,经熔炼,将成大业!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

Hello.cs


using System;
using System.Text;

namespace RemoteHello
{
    
public class Hello:System.MarshalByRefObject
    {
    
        
public Hello()
        {
        }

        
~Hello()
        {
            Console.WriteLine(
"Destructor called");
        }

        
public string HelloWorld(string name)
        {
            Console.WriteLine(
"Hello World!");
            
return "Hi," + name;
        }
    }
}

HelloClient.cs : 如果是返回System.MarshalByRefObject 的对象,要对其进行操作,可能需要注释掉的代码

using System;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Xml;
using System.IO;
using RemoteHello;

using news.dbxml;

namespace HelloClient
{
    
class HelloClient
    {

        
// first run HelloServer, then run this.
        static void Main(string[] args)
        {

            
/*
            BinaryClientFormatterSinkProvider clientSinkProvider = new BinaryClientFormatterSinkProvider();
            //clientSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            IDictionary properties = new Hashtable();
            properties["name"] = "tcp8085";
            properties["port"] = 8085;
            
            TcpClientChannel   channel = new TcpClientChannel(properties, clientSinkProvider);
            ChannelServices.RegisterChannel(channel);

            
*/

            ChannelServices.RegisterChannel(
new TcpClientChannel());
            Hello obj 
= (Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8085/HelloWorld");
            
            
            
if (obj == null)
            {
                Console.WriteLine(
"False to Link Server.");
                
return;
            }
            
else
            {
                Console.WriteLine(
"True to Link Server.");
            }

            
string str=obj.HelloWorld(" zhw");
            Console.WriteLine(str);
            
            Console.WriteLine(
"Pentry any key to continue ");
            Console.ReadLine();
        }

    }
}


HelloServcer.cs

using System;
//using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;
using news.dbxml.service.impl;

using news.dbxml;


namespace HelloService
{
    
class HelloServer
    {
        
static void Main(string[] args)
        {
            
/*
            BinaryServerFormatterSinkProvider serverSinkProvider = new BinaryServerFormatterSinkProvider();
            serverSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
            
            IDictionary properties = new Hashtable();
            properties["port"] = 8085;

            TcpServerChannel channel = new TcpServerChannel(properties,    serverSinkProvider);
             * 
*/

            TcpServerChannel channel 
= new TcpServerChannel(8085);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Hello), "HelloWorld", WellKnownObjectMode.SingleCall);

            System.Console.WriteLine(
"Press Any Key to Exit ! ");
            System.Console.ReadLine();
        }
    }
}


posted on 2007-10-18 17:01  血狼  阅读(308)  评论(0)    收藏  举报