• 关于Remoting的个人使用心得


          最经几天比较闲写了一个基于Tcp网络通信的聊天程序,写的过程中实现了文件传输,可是却怎样也无法将文件名传送过去,期间想过用通信的端口发送文件的名称,但是又要自己定义一个协议,觉得那样比较麻烦,于是想到了Remoting。 

          于是就开始了Remoting的研究之路 ,只是简单的用了一下,也只是入门希望可以帮助到刚入门Remoting的同学们。

          在这里我写了一个模板类 ,你们拿去可以直接使用 废话不多说了 全是干货。

    这是一个自己写的一个模版类

     1 using System;
     2 using System.Runtime.Remoting;
     3 using System.Runtime.Remoting.Channels;
     4 using System.Runtime.Remoting.Channels.Tcp;
     5 
     6 namespace NetChatLib
     7 {
     8     /// <summary>
     9     /// 用于Remoting的连接与释放
    10     /// </summary>
    11     public class Remoting_Helper
    12     {
    13         /// <summary>
    14         /// 服务端激活Remoting
    15         /// </summary>
    16         /// <typeparam name="T">类的类型</typeparam>
    17         /// <param name="port">端口号</param>
    18         /// <returns></returns>
    19         public bool Serivce<T>(int port)
    20         {
    21             //这里将通过反射获取T的类型的字符串 
    22             T type = (T)Activator.CreateInstance(typeof(T));
    23             //这里需要注意子类即使重写了GetType 也不行 
    24             //系统调用的仍然是Object的GetTtpe
    25             string str = type.GetType().ToString();
    26             str = str.Split('.')[1];
    27             try
    28             {
    29                 TcpServerChannel chnl = new TcpServerChannel("talkChannel", port);
    30                 ChannelServices.RegisterChannel(chnl, true);
    31                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(T), str, WellKnownObjectMode.SingleCall);
    32                 return true;
    33             }
    34             catch
    35             {
    36                 return false;
    37             }
    38 
    39         }
    40         /// <summary>
    41         /// 客户端激活Remoting
    42         /// </summary>
    43         /// <typeparam name="T">类的类型</typeparam>
    44         /// <param name="url">类型所在宿主位置</param>
    45         /// <returns>该类的实例</returns>
    46         public T Client<T>(string url)
    47         {
    48             try
    49             {
    50                 TcpClientChannel chnl = new TcpClientChannel();
    51                 ChannelServices.RegisterChannel(chnl, true);
    52                 T cls = (T)Activator.GetObject(typeof(T), url);
    53                 return cls;
    54 
    55             }
    56             catch
    57             {
    58                 return default(T);
    59             }
    60 
    61         }
    62     }
    63 }

    这是使用的方法 

    客户端  DemoClass是我测试的一个类

    1 static void Main(string[] args)
    2 {
    3     string url = "tcp://172.16.22.22:12346/DemoClass";
    4     DemoClass cls = new Remote_help().Client<DemoClass>(url);
    5     cls.SetName("你好世界");
    6     Console.Read();
    7 }

    服务器端 

    1 static void Main(string[] args)
    2 {
    3     new Remote_help().Serivce<DemoClass>(12346);
    4     Console.WriteLine("服务开启");
    5     Console.ReadKey();
    6     Console.WriteLine(new DemoClass().GetName());
    7     Console.ReadKey();
    8 }

    我的DemoClass测试类

     1 public class DemoClass : MarshalByRefObject
     2 {
     3     private int count = 0;
     4     private static string name;
     5 
     6 
     7     public void SetName(string name)
     8     {
     9         DemoClass.name = name;
    10     }
    11     public string GetName()
    12     {
    13         return DemoClass.name;
    14     }
    15 
    16     public DemoClass()
    17     {
    18         Console.WriteLine("------DemoClassConstructor-------");
    19     }
    20     public void showCount(string name)
    21     {
    22         count++;
    23         Console.WriteLine("{0} The Count is {1}", name, count);
    24     }
    25     public void showAppDomain()
    26     {
    27         AppDomain currentDomain = AppDomain.CurrentDomain;
    28         Console.WriteLine(currentDomain.FriendlyName);
    29     }
    30     public int GetCount()
    31     {
    32         return count;
    33     }
    34 }

     最近使用发现了一个问题 :在不同计算机上启动Remoting服务失败的结果 

    解决方法:将ChannelServices.RegisterChannel(chnl, true);true 改为false 

    如果还是不行,就换Http协议进行传输就好了

  • 相关阅读:
    JS实现继承,封装一个extends方法
    JS实现new关键字的功能
    前端常见原生方法的实现(bind,promise,new,extends,深拷贝,函数防抖,函数节流)
    Nodejs ORM框架Sequelize快速入门
    Nodejs ORM框架Sequelize(模型,关联表,事务,循环,及常见问题)
    NodeJs mysql 开启事务
    web开发的跨域问题详解
    docker网络
    docker容器的学习
    路飞学城的部署
  • 原文地址:https://www.cnblogs.com/qulianqing/p/6567705.html
Copyright © 2020-2023  润新知