Ice开发初步——HelloWorld的实现

初识ICE,试着使用slice语言在vs2010下编写HelloWorld程序,参考了官网http://www.zeroc.com/index.html及网络帖子上的思路,终成。现在记录一下。

1.在官网上下载最新版本的ice开发包http://www.zeroc.com/download.html,最新为Ice-3.4.2.msiIce-3.4.2-ThirdParty.msi。下载后解压到我本地E盘ICE文件夹下。

2.在IDE中设置项目需要包含的目录,vs设置全局目录的方式不同与之前版本。设置方法是:

在IDE中,打开视图-》属性管理器。展开任意一个项目的树形后,有一个名为“Microsoft.Cpp.Win32.user”的项目(如下图)。这个就是全局目录了。将ICE的bin目录添加进可执行文件目录,include添加进包含目录,lib添加进库目录

 

3 将slice2cpp作为外部工具添加到vs2010.

具体步骤:在IDE的工具-》外部工具-》添加 中,设置如下图

4 创建名为Printer的slice文件,用记事本编写即可,注意文件保存为.ice后缀形式。Printer.ice的内容如下:

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

5创建名为HelloWorld的空白解决方案。

在新建项目->其他项目类型->visual studio解决方案。

6在空白解决方案中添加名为Ice的空白静态库项目。

将Printer.ice放置到Ice项目文件夹下,然后在Ice项目的资源文件中添加现有项Printer.ice.然后执行slice2cpp工具命令。

此时Ice项目文件夹下已增加了Printer.h和Printer.cpp文件,将它们分别添加到源文件和头文件的文件夹中,这里还需要将源文件中的#include<Printer.h>改为#include"Printer.h"

生成项目,通过!

7添加一个名为Client的win32控制台应用程序的空白项目。

在属性页配置属性->链接器->输入->附加依赖项加加入iced.lib和iceutild.lib

在属性页的通用属性->框架和应用中添加新应用选择Ice,为项目的源文件文件夹下添加名为Client.cpp的源文件。内容为:

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


using namespace std;
using namespace Demo;


int main(int argc, char * 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 "Invalid proxy";
  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 status;
}

编译,通过!

3建立名为Server的空白win32控制台程序。

在属性页配置属性->链接器->输入->附加依赖项加加入iced.lib和iceutild.lib

在属性页的通用属性->框架和应用中添加新应用选择Ice,为项目的源文件文件夹下添加名为Server.cpp的源文件。内容为:

#include "../Ice/Printer.h"
#include <Ice/Application.h>
#include <Ice/Ice.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  main(int argc, char* argv[])
{
 int status = 0;
 Ice::CommunicatorPtr ic;
 
 try 
 {
  ic = Ice::initialize(argc, argv);
  Ice::ObjectAdapterPtr adapter= ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "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 status;
}

编译,通过。

4在项目文件夹下的debug目录下先运行Server.exe,再运行Client.exe。

运行是提示少3个dll文件ice34d.dll、iceutil34d.dll、和bzip2d.dll,从ICE目录下找到并复制过去,注意要复制对应vs2010的vc100目录下的dll文件。

先运行Server.exe,再运行Client.exe,在Server端出现了HelloWorld,成功!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章