Ice 分布式 helloword详解

本文详细介绍如何使用Ice在VS2005环境下搭建一个简单的分布式HelloWorld应用,包括环境配置、Slice文件编写与编译、创建Server与Client端工程的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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运行窗口会一闪而过
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值