免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1193 | 回复: 0
打印 上一主题 下一主题

请教:java编写corba,客户端问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-07 11:44 |只看该作者 |倒序浏览
我刚刚学corba,试着用java编写了一点简单的程序,在编写实现DSI时遇见问题,Server段好像没问题,但运行Client端会报错,不知道是那里边错了,请大家帮忙看看,谢谢!!!

  1. Hello.idl:
  2. module HelloApp
  3. {
  4.     interface Hello
  5.     {
  6.         string printHelloArgs(in string arg1, in short arg2);
  7.     };
  8. };

复制代码

  1. HelloServer.java
  2. import HelloApp.*;

  3. import java.util.*;
  4. import org.omg.CosNaming.*;
  5. import org.omg.CosNaming.NamingContextPackage.*;
  6. import java.io.*;
  7. import org.omg.CORBA.*;

  8. // servant 必须扩展 DynamicImplementation
  9. class HelloImpl extends DynamicImplementation
  10. {

  11.     // 存储已实现的接口的存储库标识符
  12.     static String[] myIDs = {"IDL:JavaIDL/DSIExample:1.0"};
  13.     ORB orb;

  14.     // 创建对 ORB 的引用
  15.     HelloImpl(ORB orb) {
  16.         this.orb = orb;
  17.     }
  18.     // 必须实现 invoke() 以处理请求
  19.     public void invoke(ServerRequest request) {
  20.         try {
  21.             System.out.println("DSI: invoke called, op = "+request.op_name());

  22.             // 创建 NVList 以保留参数
  23.             NVList nvlist = orb.create_list(0);

  24.             // 每个方法名都需要如下 if 语句
  25.             if (request.op_name().equals("printHelloArgs") == true) {

  26.                     // 每个参数都需要 Any
  27.                     Any any1 = orb.create_any();
  28.                     any1.insert_string("");
  29.                     nvlist.add_value("arg1", any1, ARG_IN.value);

  30.                     Any any2 = orb.create_any();
  31.                     any2.insert_string("");
  32.                     nvlist.add_value("arg2", any2, ARG_IN.value);

  33.                     // 将 NVList 传递到请求以获取值
  34.                     request.params(nvlist);

  35.                     System.err.println("Argument 1: In value: " + nvlist.item(0).value().extract_string());

  36.                     System.err.println("Argument 2: In value: " + nvlist.item(1).value().extract_short());

  37.                     TypeCode result_tc = orb.get_primitive_tc(TCKind.tk_void);
  38.                     Any result_any = orb.create_any();
  39.                     result_any.type(result_tc);

  40.                     request.result(result_any);
  41.             }
  42.         }
  43.         catch (Exception ex) {
  44.             ex.printStackTrace();
  45.             System.out.println("DSIExample: Exception thrown: " + ex);
  46.         }
  47.     }


  48. // 实现 an _ids 方法以返回接口的存储库标识符
  49. public String[] _ids() {
  50.         return myIDs;
  51. }
  52. }
  53. // 以常规方式实现的 HelloServer
  54. public class HelloServer {

  55.     public static void main(String args[])
  56.     {
  57.         try{
  58.             // 创建并初始化 ORB
  59.             ORB orb = ORB.init(args, null);

  60.             // 创建 servant 并注册到 ORB
  61.             HelloImpl helloRef = new HelloImpl(orb);
  62.             orb.connect(helloRef);
  63.             File fi = new File("ior");
  64.             java.io.OutputStream f = new java.io.FileOutputStream(fi);
  65.             java.io.DataOutputStream out = new java.io.DataOutputStream(f);
  66.             String ior = orb.object_to_string(helloRef);
  67.             out.writeBytes(ior);
  68.             out.close();

  69.             System.out.println("IOR is "+ ior);

  70.             // 等待客户机调用
  71.             orb.run();

  72.         } catch (Exception e) {
  73.             System.err.println("ERROR: " + e);
  74.             e.printStackTrace(System.out);
  75.         }
  76.     }
  77. }

复制代码

  1. HelloClient.java
  2. import java.io.*;
  3. import HelloApp.*;
  4. import org.omg.CORBA.*;
  5. import org.omg.CosNaming.*;
  6. import org.omg.PortableServer.*;

  7. public class HelloClient{
  8.         public static void main(String[] args){
  9.                 try{
  10.                         ORB orb = ORB.init(args,null);
  11.                         POA rootpoa = (POA)orb.resolve_initial_references("RootPOA");
  12.                         rootpoa.the_POAManager().activate();
  13.                        
  14.                         FileInputStream fis = new FileInputStream("ior");
  15.                         java.io.DataInputStream dis = new java.io.DataInputStream(fis);
  16.                         String ior = dis.readLine();
  17.                         System.out.println(ior);
  18.                         org.omg.CORBA.Object obj = orb.string_to_object(ior);
  19.                         Hello helloRef = HelloHelper.narrow(obj);
  20.                         System.out.println("+"+obj);
  21.                         String sin = "string";
  22.                         short shin = 3;
  23.                         String str = helloRef.printHelloArgs(sin,shin);
  24.                         System.out.println(str);
  25.                         System.out.println("remote call success");
  26.                     }
  27.                     catch(Exception e){
  28.                             e.printStackTrace(System.out);
  29.                            
  30.                             }
  31.                 }
  32. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP