---
title: "Java Corba实例"
author: "xiems"
date: "2005-12-11T20:47:00Z"
tags: []
slug: "java-corba"
draft: false
lastmod: "2005-12-11T20:47:00Z"
id: 1055177
url: "https://xiems.iteye.com/blog/1055177"
---

下载并设置java platform这里就不详细讲了。

这里以一个java的corba实例说明corba这种c-s结构的程序的实现。

NB:这篇文章虽然是用java语言写的，但是由于corba的多语言特性，idl文件即可以用java编译器编译，也可以使用其他支持corba的语言编译例如c#. 把以下的java 格式改成c#风格照样可以使用。而且c-s没什么联系，你也可以用c#写server，用java写client，这就是corba强悍的地方![](/fckeditor/editor/images/smiley/msn/shades_smile.gif)！

1）首先要熟悉idlj 语言，这个是专门进行接口设计的语言，它与java没关系，有自己的语法，具体的规则需要大家自己再网上研究，这里不多说了（或者访问如下网站详细察看[http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx\_pguide/IDL.html](http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx_pguide/IDL.html)）。

module HelloApp  
{

interface Hello  
{  
string sayHello();  
oneway void shutdown();  
};  
};

这里定义了一个简单的interface, 将其保存为hello.idl, 然后再dos命令框里面输入 idlj.exe -fall hello.idl 编译。之后会出现一个叫做HelloApp的目录，corba就是通过这个目录里面的类来进行c-s之间的数据沟通。

2）下一步，就是我们的server端：

// A server for the Hello object

import HelloApp.\*;  
import org.omg.CosNaming.\*;  
import org.omg.CosNaming.NamingContextPackage.\*;  
import org.omg.CORBA.\*;  
import org.omg.PortableServer.\*;  
import org.omg.PortableServer.POA;  
import java.util.Properties;

public class HelloServer {

public static void main(String args\[]) {  
try{  
// create and initialize the ORB  
ORB orb = ORB.init(args, null);

// get reference to rootpoa & activate the POAManager  
POA rootpoa =  
(POA)orb.resolve\_initial\_references("RootPOA");  
rootpoa.the\_POAManager().activate();

// create servant and register it with the ORB  
HelloImpl helloImpl = new HelloImpl();  
helloImpl.setORB(orb);

// get object reference from the servant  
org.omg.CORBA.Object ref =  
rootpoa.servant\_to\_reference(helloImpl);

// and cast the reference to a CORBA reference  
Hello href = HelloHelper.narrow(ref);

// get the root naming context  
// NameService invokes the transient name service  
org.omg.CORBA.Object objRef =  
orb.resolve\_initial\_references("NameService");  
// Use NamingContextExt, which is part of the  
// Interoperable Naming Service (INS) specification.  
NamingContextExt ncRef =  
NamingContextExtHelper.narrow(objRef);

// bind the Object Reference in Naming  
String name = "Hello1";  
NameComponent path\[] = ncRef.to\_name( name );  
ncRef.rebind(path, href);

System.out.println  
("HelloServer ready and waiting ...");

// wait for invocations from clients  
orb.run();  
}

catch (Exception e) {  
System.err.println("ERROR: " + e);  
e.printStackTrace(System.out);  
}

System.out.println("HelloServer Exiting ...");

} //end main  
} // end class

将其保存为HelloServer.java.放在刚才的hello.idl的目录。编译这个文件就不多说了。

3）还记得在hello中定义的interface吗？我们需要对自己定义的接口中的方法进行实现，因此HelloImp.java

// The servant -- object implementation -- for the Hello  
// example. Note that this is a subclass of HelloPOA, whose  
// source file is generated from the compilation of  
// Hello.idl using j2idl.

import HelloApp.\*;  
import org.omg.CosNaming.\*;  
import org.omg.CosNaming.NamingContextPackage.\*;  
import org.omg.CORBA.\*;  
import org.omg.PortableServer.\*;  
import org.omg.PortableServer.POA;

import java.util.Properties;

class HelloImpl extends HelloPOA //必须继承这个类，在helloApp目录中已自动生成

{  
private ORB orb;

public void setORB(ORB orb\_val) {  
orb = orb\_val;  
}

// implement sayHello() method  
public String sayHello()  
{  
return "\\nHello world !!\\n";  
}

// implement shutdown() method  
public void shutdown() {  
orb.shutdown(false);  
}  
} //end class  
同样放在server所在目录中。

4）接下来是客户端（HelloClient.java）：

// A sample Java IDL object client application.  
import HelloApp.\*;  
import org.omg.CosNaming.\*;  
import org.omg.CosNaming.NamingContextPackage.\*;  
import org.omg.CORBA.\*;

public class HelloClient  
{  
static Hello helloImpl;  
String \[] x=new String\[6];  
public static void main(String args\[]){  
try{  
// create and initialize the ORB  
ORB orb = ORB.init(args, null);

System.out.println("ORB initialised\\n");

// get the root naming context  
org.omg.CORBA.Object objRef =  
orb.resolve\_initial\_references("NameService");

// Use NamingContextExt instead of NamingContext,  
// part of the Interoperable naming Service.  
NamingContextExt ncRef =  
NamingContextExtHelper.narrow(objRef);

// resolve the Object Reference in Naming  
String name = "Hello1";  
helloImpl =  
HelloHelper.narrow(ncRef.resolve\_str(name));

System.out.println  
("Obtained a handle on server object: "  
\+ helloImpl);  
System.out.println(helloImpl.sayHello());  
helloImpl.shutdown();

}  
catch (Exception e) {  
System.out.println("ERROR : " + e) ;  
e.printStackTrace(System.out);  
}  
} //end main

} // end class

这个文件最好放在一个新建的目录，已表示和server有区别，放在一起也没有关系。如果分开的话，记着把HelloApp这个目录复制到client的目录来。

5）好啦！已经可以开始爽了，我们编译所有的java文件

6）再dos窗口输入orbd.exe –ORBInitialPort 1234（端口号可以自定义，但是记得s-c要保持一致）,启动corba服务。

7）启动服务器：java HelloServer –ORBInitialPort 1234 –ORBInitialHost localhost

8）启动客户端：java HelloClient –ORBInitialPort 1234 –ORBInitialHost localhost

9）严格执行上述过程是应该直接成功的。 已经经过测试。

10）然后再仔细研究这段代码，你就会发现corba的奥秘了。