用.Net Remoting事件实现广播

服务器端主要代码:
[STAThread]
  static void Main(string[] args)
  {
   Application.Run(new MainForm());
  }
  Remoting rem = null;
  private void MainForm_Load(object sender, System.EventArgs e)
  {
   System.Runtime.Remoting.RemotingConfiguration.Configure("MyServer.exe.config");
   rem = new Remoting();
   ObjRef objRef = RemotingServices.Marshal(rem,"Remoting");   
  }
  private void btnAdd_Click(object sender, System.EventArgs e)
  {
   if(tbMsg.Text != string.Empty)
   {
    rem.Send(tbMsg.Text);
   }
  }

服务器端配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.runtime.remoting>
        <application name="MyServer">
            <service>
                <wellknown type="MyRemote.Remoting,Remoting" objectUri="Remoting" mode="Singleton"/>               
            </service>
            <channels>    
                <channel ref="tcp" port="4037">
     <serverProviders>
      <formatter ref="binary" typeFilterLevel="Full" />
     </serverProviders>
    </channel>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>


Remoting对象代码:
using System;
using System.Data;
using System.Data.SqlClient;
namespace MyRemote
{
 public delegate void Reload(string Msg); 
 public class Remoting:MarshalByRefObject,IRemoting
 {
  public event Reload reload;
  /// <summary>
  /// 向所有订阅者发送信息
  /// </summary>
  /// <param name="Msg">信息内容</param>
  public void Send(string Msg)
  {
   try
   {
    if(reload != null)//至少有一人订阅了该事件
    {
     Reload tmpreload = null;//依次向订阅事件的人发信息
     foreach(Delegate del in reload.GetInvocationList())
     {
      tmpreload = (Reload)del;
      tmpreload(Msg);
     }
    }
   }
   catch(Exception ex)
   {
    System.Diagnostics.Debug.Write(ex.Message);return;
   }
  }

public override object InitializeLifetimeService()
  {
   return null;
  }
 }
 public interface IRemoting
 {
  event Reload reload;
  void Send(string Msg);
 }
 public class Wapper:MarshalByRefObject
 {
  public event Reload Recv;
  /// <summary>
  /// 向客户端发送信息
  /// </summary>
  /// <param name="message">信息内容</param>
  public void BroadCasting(string message)
  {
   //激发客户端开始接收信息
   Recv(message);
  }
  public override object InitializeLifetimeService()
  {
   return null;
  }
 }
}


客户端主要代码:
[STAThread]
  static void Main(string[] args)
  {
   Application.Run(new MainForm());
  }
  public Remoting rem = null;
  private void MainForm_Load(object sender, System.EventArgs e)
  {
   System.Runtime.Remoting.RemotingConfiguration.Configure("MyClient.exe.config"); 
   rem = (Remoting)Activator.GetObject(typeof(IRemoting),"tcp://127.0.0.1:4037/Remoting");
   Wapper wapper = new Wapper();
   wapper.Recv+=new Reload(LocalRecv);//收取服务器端广播来的消息
   rem.reload+=new Reload(wapper.BroadCasting);//向服务器端订阅消息
  }
  private void LocalRecv(string Msg)
  {
   tbMsg.Text += Msg;//Environment.NewLine;
  }

客户端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <client name = "MyClient">
        <wellknown type="MyRemote.Remoting,Remoting" url="tcp://127.0.0.1:4037/Remoting" />       
      </client>
      <channels>
  <channel ref="tcp" port="0">
   <clientProviders>
    <formatter ref="binary" />
   </clientProviders>
   <serverProviders>
    <formatter ref="binary" typeFilterLevel="Full" />
   </serverProviders>
  </channel>
   </channels>
    </application>
  </system.runtime.remoting>
</configuration>
posted on 2007-12-13 22:15 gguowang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/gguowang/archive/2007/12/13/994210.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值