在VS2010中配置好了ICE3.4.2,本以为程序就可以很好的运行了,在运行的时候还是发现了不少的问题,还是详细说一些过程吧
参看的是Ice 3.4.2的设计文档,虽然是英文的 但是如果要写Ice程序的话,还是好好看两眼吧,那个中文版的优点太落后了。
我们接下来讲的一些都是配置好ICE的基础上实现的。
- 打开VS2010,新建一个项目,不妨命名为“Server”,在源文件处添加 新建项,这个时候“已安装的模板”的最下方为“Slice”, 选中它,添加Slice File(.ice) 命名为Printer.ice,内容为:
module Demo{ interface Printer{ void printString(string s); }; };
- 编译此ice文件,我采用的办法是到对应的目录下,使用命令行来进行编译,编译命令也就是slice2cpp Printer.ice;经过编译,我们得到两个文件Printer.h和Printer.cpp,将这两个文件作为现有项添加到Server项目中,
- 添加一个新的Server.cpp文件,内容是参照Ice 3.4.2的设计文档得到的:
#include <Ice/Ice.h> #include "Printer.h" #include <iostream> 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; }
这里有个小问题需要注意一下,就是原文中使用的是"#include<Printer.h>",应该改用双引号#include"Printer.h",原因可能是<>到系统指定的一些目录中查找文件,但是“”找的范围更大些。 - 编译此项目,我在第一次编译的时候出了很多问题,原因就是忘了添加附加依赖项,也就出了很多链接无法解析的问题,将iced.lib和iceutild.lib添加进去后,就没有了这些问题。
上面的工作主要用于完成服务器端的任务,而剩下的就是客户端的任务了,
- 首先还是新建一个项目,命名为Client
- 当然其次还是要配置项目的环境属性
- 我在这还是将Server项目下的Printer.h和Printer.cpp复制进了这个新的项目中,而且只复制Printer.h也是有问题的,会出现很多链接无法解析
- 添加Client.cpp文件,内容如下:
#include<Ice/Ice.h> #include "Printer.h" #include <iostream> #include <string> 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; }
- 编译这个项目,我在这还是遇到了挺多问题,还是链接无法解析:1>Printer.obj : error LNK2019: 无法解析的外部符号 __imp___CrtDbgReportW,该符号在函数 "public: bool __thiscall std::_Tree_const_iterator<class std::_Tree_val<class std::_Tmap_traits<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,0> > >::operator==(class std::_Tree_const_iterator<class std::_Tree_val<class std::_Tmap_traits<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,0> > > const &)const " (??8?$_Tree_const_iterator@V?$_Tree_val@V?$_Tmap_traits@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@$0A@@std@@@std@@@std@@QBE_NABV01@@Z) 中被引用
1>C:\Users\moonbird\Documents\Visual Studio 2010\Projects\Client\Debug\Client.exe : fatal error LNK1120: 1 个无法解析的外部命令 - 这个是因为没有把代码生成中的运行库改成 “多线程调试DLL(/MDd)”,改成这个以后就没有问题了。
- 分别运行server和client
- Hello World!