The Internet Communications Engine (Ice) 跨平台异构通讯方案 第二弹-Hello world!
如果不知道ICE是什么的同学,请看上一篇的ICE简介:http://www.cnblogs.com/winds/p/3864677.html
好了,HelloWorld,从中间语言讲起。
首先,我们新建一个控制台项目,添加一个txt文件,在其中写上中间语言代码:
#ifndef HELLO_ICE #define HELLO_ICEmodule Demo {interface Hello {void sayHello(int delay);void shutdown(); };};#endif
这是一段很简单的代码,其中
#ifndef HELLO_ICE #define HELLO_ICE#endif
这是ICE的一个的规范而已,必须要加,否则接下来的编译器会通不过。这个语法和.Net的条件编译不一样,更多的像C中的代码风格。
module Demo { }
对应的是.net中的命名空间
interface Hello {void sayHello(int delay);void shutdown(); };
这个是一个简单的接口。简单的介绍完后,接下来的我们把txt后缀改为.ice后缀。
接下来,右键项目:
将EnableBuilder勾上,接下来ICE会自动帮我们生成Hello.cs文件:
好了,这个Hello.cs文件有我们定义好的被编译为C#的接口,接下里我们开始让服务端实现接口:
public class HelloI : HelloDisp_ {public override void sayHello(int delay, Ice.Current current){if(delay > 0){System.Threading.Thread.Sleep(delay);}System.Console.Out.WriteLine("Hello World!");}public override void shutdown(Ice.Current current){System.Console.Out.WriteLine("Shutting down...");current.adapter.getCommunicator().shutdown();} }
其中HelloDisp_为Hello.cs文件中被编译为C#了的一个抽象类,我们实现接口,接下来,我们了解一下ICE的基本配置(好吧,又是配置),不过这个配置比起WCF简单多了:
Hello.Endpoints=tcp -p 10000:udp -p 10000
当然我们要新建一个配置文件,名字可以随便取。这里我取的名字为:config.server 放在项目的根目录下:
接下来,写服务端:
public class Server {class App : Ice.Application{public override int run(string[] args){if(args.Length > 0){System.Console.Error.WriteLine(appName() + ": too many arguments");return 1;}Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Hello");adapter.add(new HelloI(), communicator().stringToIdentity("hello"));adapter.activate();communicator().waitForShutdown();return 0;}}public static int Main(string[] args){App app = new App();app.main(args, "config.server");return 1;} }
OK了,服务端写完了,接下来写客户端:
public class Client {public class App : Ice.Application{private static void menu(){Console.Write("usage:\n" +"t: send greeting as twoway\n" +"o: send greeting as oneway\n" +"O: send greeting as batch oneway\n" +"d: send greeting as datagram\n" +"D: send greeting as batch datagram\n" +"f: flush all batch requests\n" +"T: set a timeout\n" +"P: set a server delay");if(_haveSSL){Console.Write("\nS: switch secure mode on/off");}Console.WriteLine("\ns: shutdown server\n" +"x: exit\n" +"?: help\n");}public override int run(string[] args){if(args.Length > 0){Console.Error.WriteLine(appName() + ": too many arguments");return 1;}try{communicator().getPluginManager().getPlugin("IceSSL");_haveSSL = true;}catch(Ice.NotRegisteredException){}HelloPrx twoway = HelloPrxHelper.checkedCast(communicator().propertyToProxy("Hello.Proxy").ice_twoway().ice_timeout(-1).ice_secure(false));if(twoway == null){Console.Error.WriteLine("invalid proxy");return 1;}HelloPrx oneway = (HelloPrx)twoway.ice_oneway();HelloPrx batchOneway = (HelloPrx)twoway.ice_batchOneway();HelloPrx datagram = (HelloPrx)twoway.ice_datagram();HelloPrx batchDatagram =(HelloPrx)twoway.ice_batchDatagram();bool secure = false;int timeout = -1;int delay = 0;menu();string line = null;do {try{Console.Out.Write("==> ");Console.Out.Flush();line = Console.In.ReadLine();if(line == null){break;}if(line.Equals("t")){twoway.sayHello(delay);}else if(line.Equals("o")){oneway.sayHello(delay);}else if(line.Equals("O")){batchOneway.sayHello(delay);}else if(line.Equals("d")){if(secure){Console.WriteLine("secure datagrams are not supported");}else{datagram.sayHello(delay);}}else if(line.Equals("D")){if(secure){Console.WriteLine("secure datagrams are not supported");}else{batchDatagram.sayHello(delay);}}else if(line.Equals("f")){communicator().flushBatchRequests();}else if(line.Equals("T")){if(timeout == -1){timeout = 2000;}else{timeout = -1;}twoway = (HelloPrx)twoway.ice_timeout(timeout);oneway = (HelloPrx)oneway.ice_timeout(timeout);batchOneway = (HelloPrx)batchOneway.ice_timeout(timeout);if(timeout == -1){Console.WriteLine("timeout is now switched off");}else{Console.WriteLine("timeout is now set to 2000ms");}}else if(line.Equals("P")){if(delay == 0){delay = 2500;}else{delay = 0;}if(delay == 0){Console.WriteLine("server delay is now deactivated");}else{Console.WriteLine("server delay is now set to 2500ms");}}else if(_haveSSL && line.Equals("S")){secure = !secure;twoway = (HelloPrx)twoway.ice_secure(secure);oneway = (HelloPrx)oneway.ice_secure(secure);batchOneway = (HelloPrx)batchOneway.ice_secure(secure);datagram = (HelloPrx)datagram.ice_secure(secure);batchDatagram = (HelloPrx)batchDatagram.ice_secure(secure);if(secure){Console.WriteLine("secure mode is now on");}else{Console.WriteLine("secure mode is now off");}}else if(line.Equals("s")){twoway.shutdown();}else if(line.Equals("x")){// Nothing to do }else if(line.Equals("?")){menu();}else{Console.WriteLine("unknown command `" + line + "'");menu();}}catch(System.Exception ex){Console.Error.WriteLine(ex);}}while (!line.Equals("x"));return 0;}private static bool _haveSSL = false;}public static int Main(string[] args){App app = new App();return app.main(args, "config.client");} }
从这个客户端明眼人应该能看出不少东西,以后再依此介绍。当然客户端文件配置:
Hello.Proxy=hello:tcp -p 10000:udp -p 10000 Ice.Default.Host=192.168.1.103
好了,启动服务端和运行客户端,效果如下:
Hello world完成。源码奉上:http://files.cnblogs.com/winds/hello.zip
转载于:https://www.cnblogs.com/HouZhiHouJueBlogs/p/3864737.html
The Internet Communications Engine (Ice) 跨平台异构通讯方案 第二弹-Hello world!相关推荐
- The Internet Communications Engine (Ice) 跨平台异构通讯方案 第一弹-ICE简介
.net中的通讯方案很多,从.net Remoting,MSMQ,Webservice,WSE,WCF等等,他们都有一个特点,易用,但是都不能跨语种异构,如果你服务端要用java开发,客户端用C#开发 ...
- ICE专题:反叛之冰 Internet Communications Engine
转自:韩磊@CSDN Marc Laukien:Object-Oriented Concepts, Inc的创办人和总裁.开放源码的ORBacus (原名OmniBroker,完全遵从CORBA的OR ...
- ice(Internet Communications Engine) window 安装与配置
2019独角兽企业重金招聘Python工程师标准>>> 什么是ice ice是一个面向对像的中间件平台,因此,这意味着ice提供了一个工具,一些api和一些类库用来构造面向对像蝗基于 ...
- Ice-E(Embedded Internet Communications Engine)移植到s3c2440A(arm9)linux(2.6.12)上的 -转
1.前言 ICE-E是ICE在嵌入式上运行的一个版本,与ICE比较如下: Category Ice 3.3.0 Ice-E 1.3.0 Thread Pool concurrency model Bi ...
- ZeroC Ice启用SSL通讯的配置
Zeroc ICE ( Internet Communications Engine )中间件号称标准统一,开源,跨平台,跨语言,分布式,安全,服务透明,负载均衡,面向对象,性能优越,防火墙穿透,通讯 ...
- Android 模块 -- 基于XMPP协议的手机多方多端即时通讯方案
目 录 基于XMPP协议的手机多方多端即时通讯方案................................................................. 1 目 录 ...
- 基于XMPP协议的手机多方多端即时通讯方案
原文地址为: 基于XMPP协议的手机多方多端即时通讯方案 基于XMPP协议的手机多方多端即时通讯方案 目 录 基于XMPP协议的手机多方多端即时通讯方案...................... ...
- 银行核心海量数据无损迁移:TDSQL数据库多源异构迁移方案
为帮助开发者更好地了解和学习分布式数据库技术,2020年3月,腾讯云数据库.云加社区联合腾讯TEG数据库工作组特推出为期3个月的国产数据库专题线上技术沙龙<你想了解的国产数据库秘密,都在这!&g ...
- uniapp H5嵌套通讯方案-webviewiframe
uniapp H5嵌套通讯方案-webview&iframe 背景 webview方案 父级 手机app调试情况 H5情况 子级(嵌套页面) 手机app调试情况 父节点是H5情况 完整代码: ...
最新文章
- Unix编程艺术——Unix哲学
- 提升效率的JQUERY(转)
- 1024-程序员节快乐!给大家发福利啦!以及向大家讲述节日由来
- Linux网卡eth0变成eth1修改方法
- CMakeList.txt中设置一个可变的变量的值(bool)
- 【已作废】基于Freeswitch的ASTPP计费系统的安装 (CentOS 7)
- 探地雷达(GPR)的 C-scan (三维图)的绘制
- 【推荐论文】基于多视角学习和个性化注意力机制的新闻推荐(附论文下载链接)...
- cie规定的标准光源_孩视宝:健康自然的光源是护眼灯发展的重点
- C#热血传奇游戏服务端再次开源更新
- postSQL使用存储过程动态查询
- 软件编码测试要点总结
- 《未来世界的幸存者》摘录
- 装箱与拆箱(TDB)
- 学简单python好学吗_python好学吗? 语法简单吗? 举个例子?
- SIPP测试使用指导
- Flutter 实现风车加载动画组件
- 字符串前面加f是什么意思?
- linux检查网络是否通畅_Linux检测网络通畅命令
- 4个小方法,让你的抖音发出来的视频更清楚
热门文章
- linux 工业 网络协议,简单了解Linux TCP/IP协议栈
- FPGA学习笔记---二进制码、独热码、格雷码分析对比
- linux下无权限安装anaconda和tensorflow-gpu
- pycharm和jupyter notebook中的快捷键
- EAST: An Efficient and Accurate Scene Text Detector(自然场景下文本识别)训练,测试
- C 语言fopen打开模式
- DataSet 读取Oracle 数据
- 在win10在使用cmder代替Linux是怎样的体验
- python __call__或者说func()()的理解
- MongoDB 数据迁移 备份 导入(自用)