当前位置:   article > 正文

.net Remoting IPC和TCP通道通讯的实现_c# ipcchannel 网络通信

c# ipcchannel 网络通信

REMOTING是.NET自带的一种RPC调用方式。主要解决多个进程间互相的调用。

原来:建立一个公用的对象,该对象在服务端声明并共享出去,各个进程可以取到这个公共的对象,并修改该对象。如希望实现一个进程调用另一个进程,那么使用代理来实现该目的。TCP连接效率较快,但是基本只能用于本机。HTTP效率较慢,但是可以用于局域网。注意,服务端和客户端必须使用相同的协议。

项目结构

新建一个接口:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ClassLibrary1
  7. {
  8.     public interface IReceiveData {
  9.          void Talk(string word);
  10.     }
  11. }

实现接口

  1. namespace ClassLibrary1
  2. {
  3. [Serializable]
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class ReceiveData : MarshalByRefObject, IReceiveData
  8. {
  9. /// <summary>
  10. /// 说话
  11. /// </summary>
  12. /// <param name="word"></param>
  13. public void Talk(string word)
  14. {
  15. System.Console.WriteLine(word);
  16. }
  17. }
  18. }

 服务端同时注册IPC 和TCP通道

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //注册IPC通道
  6. IpcServerChannel channel = new IpcServerChannel("testIpc"); //端口随便取
  7. ChannelServices.RegisterChannel(channel, true);
  8. //注册远程对象
  9. RemotingConfiguration.RegisterWellKnownServiceType(
  10. typeof(ReceiveData),
  11. "ReceiveData",
  12. WellKnownObjectMode.SingleCall);
  13. //注册Tcp通道
  14. TcpServerChannel tcpChannel = new TcpServerChannel(809); //端口随便取
  15. ChannelServices.RegisterChannel(tcpChannel, true);
  16. //注册远程对象
  17. RemotingConfiguration.RegisterWellKnownServiceType(
  18. typeof(ReceiveData),
  19. "SenData",
  20. WellKnownObjectMode.SingleCall);
  21. Console.WriteLine("服务 启动");
  22. Console.ReadLine();
  23. }
  24. }

客户端只需引用接口库便可以调用两个服务接口了:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.Remoting;
  9. using System.Runtime.Remoting.Channels;
  10. using System.Runtime.Remoting.Channels.Ipc;
  11. using System.Runtime.Remoting.Channels.Tcp;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using ClassLibrary1;
  16. namespace WindowsFormsApp1
  17. {
  18. public partial class Form1 : Form
  19. {
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. try
  24. {
  25. IpcClientChannel ipcClient = new IpcClientChannel();
  26. ChannelServices.RegisterChannel(ipcClient, true);
  27. IReceiveData logFun = (IReceiveData)RemotingServices.Connect(typeof(IReceiveData), "ipc://testIpc/ReceiveData");
  28. logFun.Talk("abcdddd");
  29. //注册通道
  30. TcpClientChannel channel = new TcpClientChannel();
  31. ChannelServices.RegisterChannel(channel, true);
  32. //获取远程对象
  33. var _talk = (IReceiveData)Activator.GetObject(typeof(IReceiveData), "TCP://localhost:809/SenData");
  34. _talk.Talk("asdfadsfasdfadsfa");
  35. }
  36. catch (Exception ex)
  37. {
  38. MessageBox.Show(ex.Message);
  39. }
  40. }
  41. }
  42. }

总结:ipc通道即连即用非常的丝滑。tcp通道首次发送会卡顿,应该是tcp握手连接导致的的。 实现效果如下。

服务端

客户端

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/847729
推荐阅读
相关标签
  

闽ICP备14008679号