ZeroC最简单的C#实例

声明:该例子为官网上的例子,这里做操作上的描写补充。

1、安装ICE。

     首先,用IE浏览器登录https://zeroc.com官网,选择右上角齿轮图标(设置),选择兼容性视图。把zeroc.com添加进去。如果不添加兼容性视图,则浏览器不能显示下载链接。选择downloads,这时候可以看到ICE3.7,下面有Windows等平台的图标。选择windows,然后下载。下载完成后,安装到C盘。在系统环境变量的PATH中,可以看到ICE的路径。在cmd中输入slice2cs -v,输出了版本号3.7.1表示安装成功。

2、 新建VS工程

这里使用vs2017。官网上新建的是控制台应用程序,这里我们使用winform。建立两个工程,一个名叫server,一个名叫client。然后,工程右键选择“管理NuGet程序包”。在第一项浏览,输入zeroc.ice.net和zeroc.icebuilder.msbuild。然后将这两个插件安装到工程里面。

3、编译slice文件

    1)在C盘新建一个空文件,名称为Printer.ice,路径为C:\ Printer.ice。

    2)在文件中写入代码,这里使用官网上的slice程序:

module Demo
{
    interface Printer
    {
        void printString(string s);
    }
}

    3)按照官网上的说法,这要把这个文件添加到工程中,点击编译就可以自动生成cs文件。但是我试了没有成功。因此使用cmd编译。只需在CMD中输入slice2cs C:\ Printer.ice,然后在我的文档下面就可以找到对应的Printer.cs文件。

    4)然后把这个文件,分别添加到两个工程里面。

4、添加接口调用代码

    1)client代码,因为是winform,所以这里的是Form1的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Demo;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (Ice.Communicator communicator = Ice.Util.initialize())
                {
                    var obj = communicator.stringToProxy("SimplePrinter:default -h localhost -p 10000");
                    var printer = PrinterPrxHelper.checkedCast(obj);
                    if (printer == null)
                    {
                        throw new ApplicationException("Invalid proxy");
                    }

                    printer.printString("Hello World!");
                    MessageBox.Show("");
                }
            }
            catch (Exception err)
            {
              //  Console.Error.WriteLine(e);
                return;
            }
        }
    }
}

   2)、添加server的代码

namespace Server
{
    public class PrinterI : Demo.PrinterDisp_
    {
        public override void printString(string s, Ice.Current current)
        {
            Console.WriteLine(s);
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (Ice.Communicator communicator = Ice.Util.initialize())
                {
                    var adapter =
                        communicator.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -h localhost -p 10000");
                    adapter.add(new PrinterI(), Ice.Util.stringToIdentity("SimplePrinter"));
                    adapter.activate();
                    communicator.waitForShutdown();
                }
            }
            catch (Exception er)
            {
                Console.Error.WriteLine(e);
                return;
            }
        }
    }
}

5 、调试

  编译完工程,就可以调试了。

  在server的PrinterI类的函数printString中打断点。

  Client的按钮中,printer.printString(“Hello World”),就是向服务器发送消息。

  启动服务器,按下button1。然后启动客户端,按下button1,这时候就进入了断点。

  至于ICE的原理,可以把各个函数都给打上断点,然后查看代码,慢慢的就可以弄明白了。

猜你喜欢

转载自blog.csdn.net/FHZZWZ/article/details/83041217
今日推荐