zoukankan      html  css  js  c++  java
  • 关于Remoting信道的通信的问题

    我是今年才毕业的应届生,找了一家公司,主要从事.net开发。公司让我从事Remoting开发,最近几天看了wayfarer的一些文章,觉得很好,自己也试着做了一个小程序,不知道为什么达不到预期的效果。
    现在把程序发上来,希望大家能为我解决问题。
    远程类:
    using System;
    using System.Runtime.Remoting;

    namespace Distribution_Framework
    {
        
    //定义广播事件的参数类
        [Serializable]
        
    public class BroadcastEventArgs : EventArgs
        
    {
            
    private string msg = null;
            
    public BroadcastEventArgs(string message)
            
    {
                msg 
    = message;
            }

            
    public string Message
            
    {
                
    get
                
    {
                    
    return msg;
                }

            }

        }

        
    public delegate void BroadcastEventHandler(object sender, BroadcastEventArgs submitArgs);
        
    public class InfoCenter : MarshalByRefObject
        
    {
            
    private static int count= 0;
            
    public InfoCenter()
            
    {
                count 
    = count+1;
                Console.Write(count.ToString());
                Console.WriteLine(
    "InfoCenter created.");
            }

            
    public override object InitializeLifetimeService()
            
    {
                
    return null;
            }

            
    public event BroadcastEventHandler Broadcaster;
            
    public void Broadcasting(string message)
            
    {
                BroadcastEventArgs e 
    = new BroadcastEventArgs(message);

                
    if (Broadcaster != null)
                
    {
                    Broadcaster(
    this, e);//发出事件
                    Console.WriteLine("Broadcast:" + e.Message);
                }

            }

        }

    }





    服务端:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;
    using System.Runtime.Serialization.Formatters;
    using System.Collections;
    //using System.Runtime.Remoting.Services;
    //using directive;

    namespace Distribution_Framework
    {
        
    class Server
        
    {
            
    public static void Main(string[] Args)
            
    {
                RemotingConfiguration.Configure(
    @"F:\Broadcast\Server\Server\Server.exe.config"); 

                
    /*IDictionary channelProps = new Hashtable();
                channelProps["name"] ="BroadCaster";
                channelProps["priority"] =1;
                channelProps["port"]=8011;

                BinaryServerFormatterSinkProvider sinkProvider = new BinaryServerFormatterSinkProvider();
                sinkProvider.TypeFilterLevel =TypeFilterLevel.Full;
                HttpServerChannel channel = new HttpServerChannel(channelProps,sinkProvider);
                ChannelServices.RegisterChannel(channel);
    */


                Console.WriteLine(
    "Server is running, Press Enter key to exit.");
                Console.ReadLine();
            

                
    /*RemotingConfiguration.RegisterWellKnownServiceType(typeof(Distribution_Framework.InfoCenter), "Broadcast", WellKnownObjectMode.Singleton);
                HttpChannel myChannel = new HttpChannel(8011);
                ChannelServices.RegisterChannel(myChannel);

                IServerChannelSink sc = myChannel.ChannelSinkChain;
                while (sc != null)
                {
                    if (sc is BinaryServerFormatterSink)
                    {
                        ((BinaryServerFormatterSink)sc).TypeFilterLevel = TypeFilterLevel.Full;
                        //break;
                    }
                    if (sc is SoapServerFormatterSink)
                    {
                        ((SoapServerFormatterSink)sc).TypeFilterLevel = TypeFilterLevel.Full;
                        //break;
                    }
                    sc = sc.NextChannelSink;
                }
                Console.WriteLine("Server is running, Press Enter key to exit.");
                Console.ReadLine();
            
    */



            }

        }

    }


    Annoncer:
    using System;
    using System.Timers;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;


    namespace Distribution_Framework
    {
        
    class Announcer
        
    {
            InfoCenter infoCenter;

            
    public static void Main(string[] Args)
            
    {
                
    /*HttpChannel channel = new HttpChannel();
                ChannelServices.RegisterChannel(channel);
                RemotingServices.Connect(typeof(InfoCenter),"
    http://localhost:8011/BroadCaster");*/

                Announcer announcer 
    = new Announcer();
                announcer.Run();
                Console.WriteLine(
    "The announcer has been started.");
                Console.ReadLine();
            }


            
    public void Run()
            
    {
                
    try
                
    {
                    RemotingConfiguration.Configure(
    @"F:\Broadcast\Announcer\Announcer\Announcer.exe.config");
                    
    //infoCenter = new InfoCenter();
                    Timer timer = new Timer(1000);
                    timer.Elapsed 
    += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
                    timer.Enabled 
    = true;
                }

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

            }


            
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            
    {
                
    string msg = "The Time is: " + DateTime.Now.ToString();
                Console.WriteLine(
    "Send Message:" + msg);
                infoCenter.Broadcasting(msg.ToString());
            }

        }

    }


    客户端:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;
    using System.Collections;
    using System.Runtime.Serialization.Formatters;

    namespace Distribution_Framework
    {
        
    class Receiver : MarshalByRefObject
        
    {
            InfoCenter infoCenter;
            
    public Receiver()
            
    {
            }


            
    public override object InitializeLifetimeService() 
            
    {
                
    return null;
            }


            
    public void Run()
            
    {
                
    try
                
    {
                    
    //RemotingConfiguration.Configure(@"F:\Broadcast\Receiver\Receiver\Receiver.exe.config");
                    HttpChannel channel = new HttpChannel();
                    ChannelServices.RegisterChannel(channel);
                    RemotingServices.Connect(
    typeof(InfoCenter),"http://localhost:8011/BroadCaseter");
                    infoCenter 
    = new InfoCenter();
                    
    //订阅信息
                    infoCenter.Broadcaster += new BroadcastEventHandler(this.BroadcastReceiver);
                    
    //infoCenter.Broadcasting("hello");
                    Console.WriteLine("Ready to Recieve Message");
                    Console.ReadLine();
                }

                
    catch(Exception ex)
                
    {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }

                
    //取消订阅
                
    //infoCenter.Broadcaster -= new BroadcastEventHandler(this.BroadcastReceiver);
            }


            
    public void BroadcastReceiver(object sender, BroadcastEventArgs args)
            
    {
                Console.WriteLine(
    "Received:" + args.Message);//打印接收信息
            }


            
    public static void Main()
            
    {
                Receiver receiver 
    = new Receiver();
                receiver.Run();
            }

        }

    }



    在客户端如果用配置文件就报错,说<channels>附进有错误。
    客户端配置文件:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       
    <system.runtime.remoting>
          
    <application>
             
    <client>
                
    <client url="http://localhost:8011/BroadCaster">
                    
    <activated type="Distribution_Framework.InfoCenter, InfoCenter"/>
                
    </client>
             
    <channels>
                
    <channel ref="http" port="0"/>
                    
    <serverProviders>
                      
    <provider ref="binary" typeFilterLevel="Full"/>
                    
    </serverProviders>
             
    </channels>
          
    </application>
       
    </system.runtime.remoting>
    </configuration>
  • 相关阅读:
    明明的随机数
    字符串大小写转换
    字符串合并处理
    【数据结构】c语言实现集合的交并差运算
    【数据结构】一元多项式
    【保存】
    {【保存】数据结构
    【java】关键字this怎么用?
    【数据结构】顺序表
    【JAVA】【作业向】第一题:本学期一班级有n名学生,m门课程。现要求对每门课程的成绩进行统计:平均成绩、最高成绩、最低成绩,并统计考试成绩的分布律。
  • 原文地址:https://www.cnblogs.com/liuwenjun830/p/378733.html
Copyright © 2011-2022 走看看