ICE专题:实战分布式的Hello Word 【原创】

有关ICE的基础知识,请参照前面的ICE专题文章。由于这些文章均来自于网络,故未发之于首页。下面仅仅给出相关link:

ICE专题:利用ICE编写程序的几个注意点

ICE专题:ICE架构

ICE专题:ICE起步

ICE专题:ICE的5个服务

ICE专题:使用C#编写ICE分布式应用程序

ICE专题:学习ICE 3.0

ICE专题:在客户端中如何定位服务器(即如何寻找代理)

ICE专题:ICE在Linux下的安装

ICE专题:ICE简介

 

目标:在Windows平台上编写第一个基于ICE的Hello Word程序。

1、安装

http://www.zeroc.com/download.html#bin下载安装文件:

Windows Installer for Visual Studio 2005

Ice-3.2.0-VC80.msi  【必需】

Everything needed to use Ice on Windows XP/Server 2003/Vista in C++, Java, C#, Visual Basic and Python. This installer includes executables, debug and release DLLs, header files, import libraries, PDBs, Java classes, sample programs, and third-party dependencies.

Visual Studio 2005 is only required for C++, C#, and Visual Basic developers. Visual Studio 2005 Express compilers are also supported.

Ice-3.2.0-ThirdParty-VC80.msi

Use this installer if you want to build Ice for C++ or Ice for Java from sources. It includes header files, debug/release DLLs, and JAR files for Ice's third-party dependencies:

  • Berkeley DB 4.5.20
  • OpenSSL 0.9.8d
  • Libbzip2 1.0.3
  • Expat 2.0.0

Ice for Java users who do not want to install the full Ice distribution may also find this installer useful, as it includes the Java classes for Berkeley DB.

 

Ice for C#

IceCS-3.2.0.tar.gz
IceCS-3.2.0.zip

The complete Ice for C# source code.

Note that, to build Ice for C#, you must first compile Ice for C++ or, alternatively, download and install an appropriate binary distribution. This is necessary because the Slice-to-C# compiler is written in C++.

 

以上的3个文件,Ice-x.x.-VC80.msi是ICE在Windows平台下的运行时文件,包含了必需的库文件,头文件,和命令行工具。有关运行时的介绍,以后的专题文章将一一介绍(随项目进度逐步介绍吧:P)。第3方库,建议安装,特别是用C++开发时。当然,如果不使用C++,就不必安装了。ICE for C#的包,是个源码库,包含了ICE for C#的实现,实际上是封装了.NET Framework与ICE运行库之间的交互,即.NET程序与C++程序的交互,会看到大量的平台调用。如果起个更好的名字,也许称之为“ICE for C# Provider/Wrapper"更好。

注意安装运行时后,需要添加环境变量ICE_HOME,并将%ICE_HOME%\bin添加到Path中:

image%7B0%7D_thumb%5B13%5D.png

image%7B0%7D_thumb%5B14%5D.png

2、建立C#控制台项目

启动VS 2005,建立两个控制台项目,一个叫Server,一个叫Client。均添加对icecs.dll的引用。

image%7B0%7D_thumb%5B9%5D.png

由于我的ICE运行时安装于C:\Ice-3.2.0,故在C:\Ice-3.2.0\bin下找到此文件,需要注意的是,一定要选择:复制到本地。

image%7B0%7D_thumb%5B10%5D.png

3、定义接口方法

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

 

将这段代码存为:Printer.ice。在此文件同目录下,创建批处理命令文件cmd1.bat,内容如下:

mkdir generated
slice2cs --output-dir generated Printer.ice

运行cmd1.bat,则会在generated 文件夹下生成Printer.cs。这里暂时不分析Printer.cs的结构,直接使用它了。将Printer.cs拷贝到新建的两个项目中,注意,你必须将项目中使用的namespace设置成相同的,例如,我将根namespace设置为Demo。

image%7B0%7D_thumb%5B15%5D.png

 

image%7B0%7D_thumb%5B16%5D.png

4、添加应用逻辑

在Client项目中添加Client.cs,内容如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace Demo
{
public class Client
{
public static void Main(string[] args)
{
int status = 0;
Ice.Communicator ic = null;
try
{
ic = Ice.Util.initialize(ref args);
Ice.ObjectPrx obj = ic.stringToProxy(
"SimplePrinter:tcp -p 12345:udp -p 12345");
PrinterPrx printer
= PrinterPrxHelper.checkedCast(obj);
if (printer == null)
throw new ApplicationException("Invalid proxy");
printer.printString("Hello World!");
}
catch (Exception e)
{
Console.Error.WriteLine(e);
status = 1;
}
if (ic != null)
{
// Clean up
//
try
{
ic.destroy();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
status = 1;
}
}
Environment.Exit(status);
}
}
}

 

在server项目中,添加Server.cs文件,内容如下:

namespace Demo
{
using System;
public class PrinterI : Demo.PrinterDisp_
{
public override void printString(string s, Ice.Current current)
{
Console.WriteLine(s);
}
}
public class Server
{
public static void Main(string[] args)
{
int status = 0;
Ice.Communicator ic = null;
try
{
ic = Ice.Util.initialize(ref args);
Ice.ObjectAdapter adapter
= ic.createObjectAdapterWithEndpoints(
"SimplePrinter", "tcp -p 12345:udp -p 12345");
Ice.Object obj = new PrinterI();
adapter.add(
obj,
Ice.Util.stringToIdentity("SimplePrinter"));
adapter.activate();
ic.waitForShutdown();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
status = 1;
}
if (ic != null)
{
// Clean up
//
try
{
ic.destroy();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
status = 1;
}
}
Environment.Exit(status);
}
}
}


5、测试应用

切换到Server项目的生成目录,发现生成了server.exe程序,运行:Server.exe。
如果你的OS安装了网络防火墙,注意开通相应的12345端口。最好关闭所有防火墙软件后,测试应用程序。
server.exe运行后,就一直处于监听状态。运行netstat -an,可以看到服务器端程序监听着12345端口。

image%7B0%7D_thumb%5B18%5D.png

在命令行下运行Client.exe,会发现Server.exe的控制台显示:Hello Word!

 

 

注意事项:

1、通信协议串请使用TCP或UDP,如以上所提供的那样,ICE手册中所给的协议是:"default -p 10000",这在我的平台下并不可用,这需要给ICE配置默认设置。ICE设置使用 【程序名.config】 文件来配置,下面给出一个例子:

#
# The client reads this property to create the reference to the
# "hello" object in the server.
#
Hello.Proxy=hello:tcp -p 10000:udp -p 10000:ssl -p 10001

#
# Warn about connection exceptions.
#
#Ice.Warn.Connections=1

#
# We want a faster ACM for this demo.
#
Ice.ACM.Client=10

#
# Network Tracing
#
# 0 = no network tracing
# 1 = trace connection establishment and closure
# 2 = like 1, but more detailed
# 3 = like 2, but also trace data transfer
#
#Ice.Trace.Network=1

#
# Protocol Tracing
#
# 0 = no protocol tracing
# 1 = trace protocol messages
#
#Ice.Trace.Protocol=1

#
# Security Tracing
#
# 0 = no security tracing
# 1 = trace messages
#
#IceSSL.Trace.Security=1

#
# SSL Configuration
#
Ice.Plugin.IceSSL=icesslcs, Version=3.2.0.0, Culture=neutral, PublicKeyToken=1f998c50fec78381:IceSSL.PluginFactory
IceSSL.DefaultDir=../../../certs
IceSSL.ImportCert.CurrentUser.Root=cacert.pem
IceSSL.CertFile=c_rsa1024.pfx
IceSSL.Password=password
Ice.ThreadPerConnection=1

2、关闭防护墙
当不关闭防火墙时,出现一些莫名其妙的问题,均是程序的TCP/IP出入受到了限制导致的。

后记:

最近项目使用了ICE来实现分布式应用,ICE的确功能强大,希望使用过的朋友或对ICE关注的朋友能一起交流交流经验,彼此提高。【开源应用技术群 25935569】 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第 1 章引言 1 1.1 引言 1 1.2 Internet Communications Engine (Ice) 3 1.3 本书的篇章结 4 1.4 排字约定 4 1.5 源码示例 5 1.6 联系作者 5 1.7 Ice 支持 5 第 Ice 综述 7 第 2 章 Ice 综述 9 2.1 本章综 9 2.2 Ice 架构 9 2.3 Ice 服务 21 2.4 Ice 在架构上提供的好处 23 2.5 与 CORBA 的对比 25 第 3 章 Hello World 应用 33 3.1 本章综 33 3.2 编写 Slice 定义 33 3.3 编写使用 C++Ice应用 34 3.4 编写使用 JavaIce 应用 41 3.5 总结 48 第 Ice 核心概念 51 第 4 章 Slice 语言 53 4.1 本章综 53 4.2 引言 53 4.3 编译 54 4.4 源文件 57 4.5 词法规则 59 4.6 基本的 Slice 类型 62 4.7 用户定义的类型 63 4.8 接口、操作,以及异常 70 4.9 类 92 4.10 提前声明 106 4.11 模块 107 4.12 类型 ID 109 4.13 Object 上的操作 110 4.14 本地类 111 4.15 Ice 模块 112 4.16 名字与作用域 113 4.17 元数据 117 4.18 使用 Slice 编译器 118 4.19 Slice 与 CORBA IDL 的对比 119 4.20 总结 127 第 5 章 一个简单文件系统的 Slice 定义 137 5.1 本章综 137 5.2 文件系统应用 137 5.3 文件系统的 Slice 定义 138 5.4 完整的定义 140 第 6 章 客户端的 Slice-to-C++ 映射 143 6.1 本章综 143 6.2 引言 143 6.3 标识符的映射 144 6.4 模块的映射 144 6.5 Ice 名字空间 145 6.6 简单内建类型的映射 146 6.7 用户定义类型的映射 146 6.8 常量的映射 150 6.9 异常的映射 151 6.10 运行时异常的映射 154 6.11 接口的映射 154 6.12 操作的映射 161 6.13 异常处理 167 6.14 类的映射 169 6.15 slice2cpp 命令行选项 183 6.16 与 CORBA C++映射比较 184 第 7 章开发 C++ 文件系统客户 189 7.1 本章综 189 7.2 C++ 客户 189 7.3 总结 194 第 8 章 客户端的 Slice-to-Java 映射 197 8.1 本章综 197 8.2 引言 197 8.3 标识符的映射 198 8.4 模块的映射 198 8.5 Ice Package 199 8.6 简单内建类型的映射 200 8.7 用户定义类型的映射 200 8.8 常量的映射 204 8.9 异常的映射 205 8.10 运行时异常的映射 206 8.11 接口的映射 207 8.12 操作的映射 213 8.13 异常处理 219 8.14 类的映射 220 8.15 Package 224 8.16 slice2java 命令行选项 225 第 9 章开发 Java 文件系统客户 229 9.1 本章综 229 9.2 Java 客户 229 9.3 总结 233 第 10 章 服务器端的 Slice-to-C++ 映射 235 10.1 本章综 235 10.2 引言 235 10.3 服务器端 main函数 236 10.4 接口的映射 247 10.5 参数传递 249 10.6 引发异常 251 10.7 对象体现 252 10.8 总结 257 第 11 章开发 C++ 文件系统服务器 261 11.1 本章综 261 11.2 实现文件系统服务器 261 11.3 总结 276 第 12 章 服务器端的 Slice-to-Java 映射 279 12.1 Chapter Overview 279 12.2 引言 279 12.3 服务器端 main函数 280 12.4 接口的映射 285 12.5 参数传递 287 12.6 引发异常 288 12.7 Tie 类 289 12.8 对象体现 292 12.9 总结 296 第 13 章开发 Java 文件系统服务器 297 13.1 本章综 297 13.2 实现文件系统服务器 297 13.3 总结 306 第 14 章 Ice 属性与配置 307 14.1 本章综 307 14.2 属性 307 14.3 配置文件 309 14.4 在命令行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值