惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
SecWiki News
SecWiki News
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
F
Fortinet All Blogs
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LINUX DO - 最新话题
N
News | PayPal Newsroom
PCI Perspectives
PCI Perspectives
Engineering at Meta
Engineering at Meta
美团技术团队
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
V
Vulnerabilities – Threatpost
B
Blog RSS Feed
NISL@THU
NISL@THU
Security Latest
Security Latest
The Register - Security
The Register - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Schneier on Security
罗磊的独立博客
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
S
Security Affairs
月光博客
月光博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - fanrsh

Javascript's Event 的一点总结 [转]SQL Server2005 SQLCLR代码安全之权限(3) [转]SQL Server2005 SQLCLR代码安全之权限(2) [转]SQL Server2005 SQLCLR代码安全之权限(1) jquery入门1:简单收缩菜单 - fanrsh - 博客园 jQuery工作原理解析以及源代码示例 ASP.NET 页面生存周期概览 推荐一个快速反射调用的类 Visual Studio 2005的版本情况和新特征详细介绍 Creating and writing ASP.NET 2.0 custom Configuration Sections 给大家一个CSS编辑工具,结合JQUERY用的很爽啊 flv视频是如何转的? codesmith学习资源 asp.net 事件验证 CSS循序渐进系列 [更新中]Lucene.net,中文分词技术 ICTCLAS研究 .net sql连接字符串详解 socket专家好文收藏 jQuery学习资源
Remoting事件序列一:客户端触发服务器端事件
fanrsh · 2007-09-14 · via 博客园 - fanrsh

最近常有网友问我关于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();
        }

    }

}

代码很简单,我就不做描述了,有问题直接留言,我会尽快做出回复..