.net remoting 实现通用消息处理窗口

.net remoting 实现通用消息处理窗口


  • 实现机制是制作一个cmd窗口作为信息展示窗口,主程序将需要展示的信息抛出到cmd窗口显示,以此方式做到消息的展示。

以下是cmd窗口的代码,cmd窗体作为一个消息接收的server

  static void Main(string[] args)
{
try
{
//声明一个TCP通道指定端口8085
TcpChannel channel = new TcpChannel();
//注册通道
ChannelServices.RegisterChannel(channel, false);
//从哪个类,发出消息
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ToolFunction.ConsoleMessage), "ConsoleMessage", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Server:Press Enter key to exit");
//等待输入
System.Console.ReadLine();
}
catch (Exception)
{
throw;
} }
  • 初始化client
 using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting; namespace ToolFunction
{
public class RemoteMessage
{
public static IMessage obj = null; /// <summary>
/// 初始化客户端
/// </summary>
public static void InitClient()
{
if (obj == null)
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel, false);
IMessage _obj = (IMessage)Activator.GetObject(typeof(IMessage), "tcp://localhost:8085/ConsoleMessage");
obj = _obj;
}
} /// <summary>
/// 发送消息
/// </summary>
/// <param name="p_strMess"></param>
public static void SendMessage(string p_strMess)
{
try
{
if (obj == null)
{
Console.WriteLine("Could not locate TCP server");
}
obj.ShowMess(p_strMess);
}
catch (Exception ex)
{
CommonFunction.WriteError(ex.ToString());
}
}
}
}
  • 此类要实现一个接口MarshalByRefObject只有实现此接口才能实现remoting操作
 using System;
using System.Collections.Generic;
using System.Text; namespace ToolFunction
{
public interface IMessage
{
void ShowMess(string p_strMess);
}
public class ConsoleMessage : MarshalByRefObject, IMessage
{
#region IMessage 成员 public void ShowMess(string p_strMess)
{
Console.WriteLine(p_strMess);
} #endregion
}
}
  • 调用示例
//打开cmd程序
string _strExePath = Application.StartupPath + @"\MessagePlatform.exe";
Process.Start(_strExePath);
RemoteMessage.InitClient();
  • 发送消息
RemoteMessage.SendMessage("测试消息" );
  • 最后 的样子是这样的.net remoting 实现通用消息处理窗口
上一篇:Android 网络连接判断与处理


下一篇:20145336张子扬 《网络对抗技术》 PC平台逆向破解