1 / 18

显示 Hello World 字符串程序

显示 Hello World 字符串程序. IDL 的定义: interface Hello { string getGreeting(); }; 在此使用的是 VisiBroker 来进行编译的, idl2cpp hello.idl 编译后产生四个文件如下: Hello_c.cc hello_c.hh Hello_s.cc hello_s.hh 在通过 Nmake –f makefile.cpp 来产生伺服程序和客户程序. 服务器端程序. #include "stdio.h" #include "hello_s.hh"

lamond
Télécharger la présentation

显示 Hello World 字符串程序

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 显示Hello World字符串程序 • IDL的定义: interface Hello { string getGreeting(); }; 在此使用的是VisiBroker来进行编译的,idl2cpp hello.idl编译后产生四个文件如下: Hello_c.cc hello_c.hh Hello_s.cc hello_s.hh 在通过Nmake –f makefile.cpp来产生伺服程序和客户程序

  2. 服务器端程序 #include "stdio.h" #include "hello_s.hh" class HelloImpl: public virtual POA_Hello { public: virtual CORBA::String getGreeting() throw(CORBA::SystemException);}; CORBA::String HelloImpl::getGreeting() throw(CORBA::SystemException) { CORBA::String hel; hel="Hello World!"; return hel; }

  3. int main(int argc, char* const* argv) { try { // 初始化ORB. CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); // 产生一个Root POA的引用 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj); // 产生一个POA管理 PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager(); //激活POA管理 poa_manager->activate();

  4. //定义一个对象 HelloImpl Hello_servant; //产生对象引用 Hello_var view_str=Hello_servant._this(); //把对象转换为字符串 CORBA::String_var str=orb->object_to_string(view_str); //输出字符串 cout<<str<<endl; //运行服务器程序 orb->run(); //等待请求 } catch(const CORBA::Exception& e) { cerr << e << endl; return 1; } return 0;}

  5. 客户端程序 #include <string.h> #include "hello_c.hh" int main(int argc, char* const* argv) { try { //初始化ORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); //定义字符串,接收服务器端的字符串 char string_obj[1000]; cin>>string_obj; string_obj[strlen(string_obj)+1]='\0'; //把得到的字符串转换为对象引用 CORBA::Object_var obj=orb->string_to_object(string_obj);

  6. if(CORBA::is_nil(obj)){ cerr<<"Nil Hello reference"<<endl; throw 0; } //对对象进行安全转换 Hello_var Hel=Hello::_narrow(obj); if(CORBA::is_nil(Hel)){ cerr<<"Argument is not a Hello reference"<<endl; throw 0; } //使用对象调用服务器中的函数,得到结果,并显示 CORBA::String view_H=Hel->getGreeting(); cout<<view_H<<endl; } catch(const CORBA::Exception& e) { cerr << e << endl; return 1; } return 0;}

  7. 运行服务器和客户程序如下: • 打开两个DOS窗口: • 在一个中运行:Server >a.txt • 在另一个中运行:Client <a.txt • 将在客户端显示结果为:Hello World!

  8. 作业中的客户端程序 int main(int argc, char* const* argv) { try { if(argc!=2) { cerr<<"Usage:client IOR_string"<<endl; throw 0; } // Initialize the ORB. CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); CORBA::Object_var obj=orb->string_to_object(argv[1]);

More Related