http://www.iloveyou10000.com

新blogs地址 http://itares.cnblogs.com

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

最近常有网友问我关于Remoting事件的问题,我也正好有些时间,于是决定写两篇关于Remoting事件的入门随笔,希望对新手有所帮助..谢谢

随笔一:客户端触发服务器端事件

通讯类:

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

using System.Runtime.Serialization.Formatters; 

namespace RemotingSamples 
{

    
public class Server
    
{
        
public static void Main(string [] args) 
        
{

            BinaryServerFormatterSinkProvider serverProvider 
= new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider 
= new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel 
= TypeFilterLevel.Full;  

            IDictionary props
=new Hashtable(); 
            props[
"port"]=8085;  

            TcpChannel chan1 
= new TcpChannel(props,clientProvider,serverProvider);


            ChannelServices.RegisterChannel(chan1);



            RemotingConfiguration.RegisterWellKnownServiceType
                (
                
typeof(HelloServer),
                
"SayHello",
                WellKnownObjectMode.Singleton
                );


            

            System.Console.WriteLine(
"Press Enter key to exit");
            System.Console.ReadLine();

        }


    }

}


服务器端代码:
using System;
using System.Collections; 
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

using System.Runtime.Serialization.Formatters; 

namespace RemotingSamples 
{

    
public class Server
    
{
        
public static void Main(string [] args) 
        
{

            BinaryServerFormatterSinkProvider serverProvider 
= new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider 
= new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel 
= TypeFilterLevel.Full;  

            IDictionary props
=new Hashtable(); 
            props[
"port"]=8085;  

            TcpChannel chan1 
= new TcpChannel(props,clientProvider,serverProvider);


            ChannelServices.RegisterChannel(chan1);



            RemotingConfiguration.RegisterWellKnownServiceType
                (
                
typeof(HelloServer),
                
"SayHello",
                WellKnownObjectMode.Singleton
                );


            

            System.Console.WriteLine(
"Press Enter key to exit");
            System.Console.ReadLine();

        }


    }

}


客户端代码:
using System;
using System.Collections; 
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
using System.Runtime.Serialization.Formatters; 

namespace RemotingSamples 
{
    
public class Client
    
{
        
public static void Main(string[] args)
        
{

            BinaryServerFormatterSinkProvider serverProvider 
= new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider 
= new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel 
= TypeFilterLevel.Full;  
            
//使用TCP通道得到远程对象
            IDictionary props = new Hashtable();
            props[
"port"= 0;
            TcpChannel chan1 
= new TcpChannel(props, clientProvider, serverProvider);
            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");
            }



            obj1.myevent 
+= new MyEventHandler(obj1.eventmethod);


            Console.WriteLine(
                
"Client1 TCP HelloMethod {0}",
                obj1.HelloMethod(
"Caveman1"));

            Console.ReadLine();
        }

    }

}


代码很简单,我就不做描述了,有问题直接留言,我会尽快做出回复..
posted on 2007-09-14 17:22  fanrsh  阅读(1096)  评论(0编辑  收藏  举报