Ice 分布式 helloword详解

来源:互联网 发布:试剂数据库 编辑:程序博客网 时间:2024/05/20 14:20

Ice 分布式 helloword详解 (vs2005)


所需要的软件 vs2005,Ice-3.3.1-VC80.msi,Ice-3.3.1-ThirdParty-VC80.msi

软件下载地址

一、环境配置
1、安装Ice-3.3.1-VC80.msi,安装目录设为C:\Zeroc,完全安装。

2、安装第三方依赖库Ice-3.3.1-ThirdParty-VC80.msi,安装目录:C:\Zeroc\ThirdParty\

3、系统环境变量
新建ICERoot: C:\Zeroc (记得重启)
path: %ICERoot%\bin;%ICERoot%\lib;%ICERoot%\ThirdParty\bin;%ICERoot%\ThirdParty\lib;
配置是否成功测试:进入命令窗口 cmd,命令:slice2cpp -v 显示安装的版本即为安装正确

二、编译slice
1、编写slice文件(任何编辑器均可)

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

保存为Printer.ice。
2、编译
进入命令窗口 cmd,进入到Printer.ice所在目录,
编译命令 slice2cpp Printer.ice 生成 Printer.h和Printer.cpp文件
这里写图片描述
这里写图片描述

三、创建HelloWord工程
1、新建空白解决方案ICE_TEST
这里写图片描述
2、在解决方案ICE_TEST中 新建项目 win32控制台应用程序 Server
(1)、将Printer.h和Printer.cpp拷贝到Server工程下,添加现有项:Printer.h , Printer.cpp到Server工程中。
(2)、修改Server.cpp,代码如下:

#include "stdafx.h"#include <Ice/Ice.h>#include <Printer.h>using namespace std;using namespace Demo;class PrinterI:public Printer{public:    virtual void printString(const string& s, const Ice::Current&);};void PrinterI::printString(const string& s, const Ice::Current&){    cout<<s<<endl;};int _tmain(int argc, _TCHAR* argv[]){    int status=0;    Ice::CommunicatorPtr ic;    try    {        ic=Ice::initialize(argc,argv);        Ice::ObjectAdapterPtr adapter=ic->createObjectAdapterWithEndpoints("SimplePrinter", "default -p 10000");        Ice::ObjectPtr object=new PrinterI;        adapter->add(object,ic->stringToIdentity("SimplePrinter"));        adapter->activate();        ic->waitForShutdown();    }    catch (const Ice::Exception& e)    {        cerr << e << endl;        status = 1;    }    catch (const char* msg)    {        cerr << msg << endl;        status = 1;    }    if (ic)    {        try         {            ic->destroy();        }        catch (const Ice::Exception& e)         {            cerr << e << endl;            status = 1;        }    }    return 0;}

(3)、Server配置属性设置
这里写图片描述
常规 –> 字符集:使用多字节字符集
c/c++ –>常规 –>附件包含目录 : .;”$(ICERoot)\include”
c/c++ –>代码生成 –>运行时库 :多线程调试 DLL (/MDd)
c/c++ –>预编译头 –>创建/使用预编译头: 不使用预编译头
链接器–>常规–>附加库目录:$(ICERoot)\lib
链接器–>输入–>附加依赖项:Iced.lib IceUtild.lib
3、在解决方案ICE_TEST中 新建项目 win32控制台应用程序 Client
(1)、将Printer.h和Printer.cpp拷贝到Client工程下,添加现有项:Printer.h , Printer.cpp到Client工程中。
(2)、修改Client.cpp,代码如下:

#include "stdafx.h"#include <Ice/Ice.h>#include <Printer.h>using namespace std;using namespace Demo;int _tmain(int argc, _TCHAR* argv[]){    int status=0;    Ice::CommunicatorPtr ic;    try{        ic=Ice::initialize(argc,argv);        Ice::ObjectPrx base=ic->stringToProxy("SimplePrinter:default -p 10000");        PrinterPrx printer=PrinterPrx::checkedCast(base);        if(!printer)            throw "Invalidproxy";        printer->printString("hello world!");    }catch(const Ice::Exception&ex){        cerr<<ex<<endl;        status=1;    }catch(const char* msg){        cerr<<msg<<endl;        status=1;    }    if(ic)        ic->destroy();    return 0;}

(3)、Client配置属性设置
这里写图片描述
常规 –> 字符集:使用多字节字符集
c/c++ –>常规 –>附件包含目录 : .;”$(ICERoot)\include”
c/c++ –>代码生成 –>运行时库 :多线程调试 DLL (/MDd)
c/c++ –>预编译头 –>创建/使用预编译头: 不使用预编译头
链接器–>常规–>附加库目录:$(ICERoot)\lib
链接器–>输入–>附加依赖项:Iced.lib IceUtild.lib

4、工程目录截图及运行图
这里写图片描述
这里写图片描述
首先运行Server.exe,在运行Client.exe.Client运行窗口会一闪而过
这里写图片描述

0 0
原创粉丝点击