gguowang

SqlServer 、API编程、Asp.Net,Winform.......

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
服务器端主要代码:
[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  阅读(1850)  评论(5编辑  收藏  举报