ICE3.4.1试用手记(kenter原创)

ICE3.4.1试用手记(kenter原创)

由于项目中服务端用的是C++,客户端是C#。想用一些比较方便而效率也不低的中间件。

看了下文章介绍ICE http://www.zeroc.com/ice.html

相关下载 http://www.zeroc.com/download.html

安装 Ice-3.4.1.msi 后,设置系统变量加入bin目录 以便试用slice命令根据ICE统一接口语言生成相应的文件

例如我试用VS 2010 配置VC++包含目录,库目录等等

包含目录加入$(SolutionDir)

在C/C++项里的“运行时库”为多线程DLL(/MD),否则在编译时会出预编译错误:

预编译头选“不使用”,并将stdafx.h和stdafx.cpp两个文件去掉,为了兼容性。

链接器->输入->附加依赖项 加入ice.lib;iceutil.lib;

下面写个范例,首先创建ICE接口文件

Printer.ice

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

 运行CMD使用命令 slice2cpp Printer.ice后

目录下生成 Printer.h 和 Printer.cpp两个文件

把这两个文件加入工程

编写服务端代码

// TestICEServer.cpp : 定义控制台应用程序的入口点。
//

#include 
<Ice/Ice.h>
#include 
<TestICEServer/Printer.h>

using namespace std;
using namespace demo;

class PrinterI : public Printer{
public:
    
virtual void printString(const ::std::string& s, 
        
const ::Ice::Current& /* = ::Ice::Current */);
};

void PrinterI::printString(const ::std::string& s, const ::Ice::Current& )
{
    cout 
<< s << endl;
}

int main(int argc, char* argv[])
{
    
int status = 0;
    ::Ice::CommunicatorPtr ic;
    
try{

    }
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;
}
原文地址:https://www.cnblogs.com/kenter/p/2010386.html