1.下载安装Ice-3.5.1.msi http://www.zeroc.com/download.html
2.添加安装路径到环境变量path,D:\Program Files (x86)\ZeroC\Ice-3.5.1\bin\vc110
3.编写一个ice文件,可以用文件编辑器
module MCal
{
interface Cal
{
int add(int num1,int num2);
int sub(int num1,int num2);
};
};
4.用cmd工具编译成特定语言,如C++
slice2cpp Caculator.ice
生成两个文件,Caculator.h,Caculator.cpp,这两个文件将包含到你的工程中.cpp可能包含.h路径不对<>,改成双引号“”
5.引用lib
#include <Ice/Ice.h>
#include <Ice/Application.h>
/*#include <iostream>
using namespace std;*/
#pragma comment(lib,"iced.lib" )
#pragma comment(lib,"iceutild.lib" )
6.工程包含路径
7.编写server工程,创建实现类
//.h
#include "caculator.h"
using namespace MCal;
class CalI :
public Cal
{
public:
CalI(void);
~CalI(void);
virtual int add(int num1,int num2,const Ice::Current&);
virtual int sub(int num1,int num2,const Ice::Current&);
private:
int num1d;
int num2d;
}; //.cpp
CalI::CalI(void)
{
num1d=0;
num2d=0;
}
CalI::~CalI(void)
{
} int CalI::add(int num1, int num2, const Ice::Current &)
{
int sumv=0;
sumv=num1+num2;
return sumv;
}
int CalI::sub(int num1,int num2,const Ice::Current&)
{
int difv=0;
if (num1>num2)
{
difv=num1-num2;
return difv;
}
else
{
cout<<"there is some wrong!"<<endl;
return 0;
}
}
8.服务器监听
#include "CalI.h"
.....main函数
Ice::CommunicatorPtr ic;
ic = Ice::initialize(); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints("Caladapter","default -p 10000");
Ice::ObjectPtr object = new CalI();
adapter->add(object,ic->stringToIdentity("Cal"));
adapter->activate();
printf( " server.start ok \n " );
ic->waitForShutdown(); if (ic)
{
ic->destroy();
}
.......
9.客户端程序,配置和服务器一样
调用
using namespace MCal;
int _tmain(int argc, _TCHAR* argv[])
{
Ice::CommunicatorPtr ic; ic = Ice::initialize();
Ice::ObjectPrx base = ic -> stringToProxy( "Cal:default -p 10000" );
CalPrx calculator = CalPrx::checkedCast(base);
if(!calculator)
throw "Invalid proxy!"; cout<<calculator->add(,)<<endl;
cout<<calculator->sub(,)<<endl; printf( " client.start ok \n " );
getchar();
if (ic)
{
ic -> destroy();
}
return ;
}
10.C#调用
slice2cs Caculator.ice 生成.cs接口文件,包含到工程
工程添加ICE引用,文件在D:\Program Files (x86)\ZeroC\Ice-3.5.1\Assemblies\Ice.dll
using Ice;
using MCal;
Ice.Communicator ic = null;
ic = Ice.Util.initialize(ref args); Ice.ObjectPrx obj = ic.stringToProxy("Cal:default -p 10000");
CalPrx calculator = CalPrxHelper.checkedCast(obj);
Console.WriteLine("1+2="+calculator.add(, ).ToString());
Console.WriteLine("56-12=" + calculator.sub(, ).ToString()); if (ic != null)
ic.destroy();
Console.ReadKey();