Corba入门程序(Java --- C)---omniORB

Hello.idl

#ifndef __HELLO_DEFINED
#define __HELLO_DEFINED

/* CmIdentification
  %X% %Q% %Z% %W% */

interface Hello {
 /*
 @roseuid 445956A100BB */
 string sayHello (
  in string str
  );
  
};

#endif
###################################################################################

java server端实现文件:(idlj -fall hello.idl生成文件生成的文件略)

import org.omg.CORBA.ORB;

public class HelloImpl extends HelloPOA {

 private ORB orb;
    public void setORB(ORB orb_val) {
        orb = orb_val;
    }
    /* 实现接口声明方法sayHello */
    public String sayHello(String message) {
        System.out.println("我在CORBA的服务器端,客户端正在调用'sayHello'方法. ");
        System.out.println("Hello  " + message);
        return message;
    }
}

####################################################################

C客户端(omniidl -bcxx SysProp.idl 生成的文件略)

#include <iostream.h>
#include "Hello.h"
//-ORBInitRef  NameService=corbaloc::168.192.1.213:1050/NameService
int main(int argc, char** argv)
{
  try {
  CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB4");
 
  CORBA::Object_var obj = orb->resolve_initial_references("NameService");
  CosNaming::NamingContext_var rootContext;
  rootContext = CosNaming::NamingContext::_narrow(obj.in());

  CosNaming::Name name;
  name.length(1);
  name[0].id   = (const char*) "Hello";      
  name[0].kind = (const char*) "";
  
  CORBA::Object_var obj1= rootContext->resolve(name);

  Hello_ptr echoref =Hello::_narrow(obj1);

  if( CORBA::is_nil(echoref) ) {
     cerr << "没有对象" << endl;
     return 1;
   }
      const char* message="hhhheeeeeereeeeeeeeeeee";
   
   CORBA::String_var dest = echoref->sayHello(message);
   cout <<  (char*)message << "=" <<(char*)dest << endl;
   orb->destroy();
    
      }
      catch(CORBA::COMM_FAILURE& ex) {
   cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
   << "object." << endl;
  }
  catch(CORBA::SystemException&) {
   cerr << "Caught CORBA::SystemException." << endl;
  }
  catch(CORBA::Exception&) {
   cerr << "Caught CORBA::Exception." << endl;
  }
  catch(omniORB::fatalException& fe) {
   cerr << "Caught omniORB::fatalException:" << endl;
   cerr << " file: " << fe.file() << endl;
   cerr << " line: " << fe.line() << endl;
   cerr << " mesg: " << fe.errmsg() << endl;
  }
  catch(...) {
   cerr << "Caught unknown exception." << endl;
  }
    
   
  return 0;
}

 

VC6运行时的参数 -ORBInitRef  NameService=corbaloc::168.192.1.213:1050/NameService

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值