below is a simple example of a corba program
download the source file
<b>1. produce a idl file like this</b>
hello.idl
module helloapp {
interface hello {
string sayhello();
};
};
<b>2. produce stub and skeleton files through idltojava.exe</b>
idltojava hello.idl
idltojava is now named as idlj.exe and is included in the jdk.
<b>3. write a server program like this </b>
// helloserver.java
import helloapp.*;
import org.omg.cosnaming.*;
import org.omg.cosnaming.namingcontextpackage.*;
import org.omg.corba.*;
import java.io.*;
class helloservant extends _helloimplbase
{
public string sayhello()
{
return "hello world !!";
}
}
public class helloserver {
public static void main(string args[])
{
try{
// create and initialize the orb
orb orb = orb.init(args, null);
// create servant and register it with the orb
helloservant helloref = new helloservant();
orb.connect(helloref);
// get the root naming context
org.omg.corba.object objref =
orb.resolve_initial_references("nameservice");
namingcontext ncref = namingcontexthelper.narrow(objref);
// bind the object reference in naming
namecomponent nc = new namecomponent("hello", "");
namecomponent path[] = ;
ncref.rebind(path, helloref);
// wait for invocations from clients
java.lang.object sync = new java.lang.object();
synchronized (sync) {
sync.wait();
}
} catch (exception e) {
system.err.println("error: " + e);
e.printstacktrace(system.out);
}
}
}
<b>4. write a client program like this</b>
// helloclient.java
import helloapp.*;
import org.omg.cosnaming.*;
import org.omg.corba.*;
public class helloclient
{
public static void main(string args[])
{
try{
// create and initialize the orb
orb orb = orb.init(args, null);
// get the root naming context
org.omg.corba.object objref =
orb.resolve_initial_references("nameservice");
namingcontext ncref = namingcontexthelper.narrow(objref);
// test
system.out.println("ok..");
// resolve the object reference in naming
namecomponent nc = new namecomponent("hello", "");
namecomponent path[] = ;
hello helloref = hellohelper.narrow(ncref.resolve(path));
// call the hello server object and print results
//string oldhello = helloref.lastmessage();
//system.out.println(oldhello);
string hello = helloref.sayhello();
system.out.println(hello);
} catch (exception e) {
system.out.println("error : " + e) ;
e.printstacktrace(system.out);
}
}
}
<b>5. complie these files</b>
javac *.java helloapp/*.java
<b>6. run the application</b>
a. first you’ve to run the name service prior to the others likethis
c:>tnameserv
b. run server
c:>java helloserver
c. run client
c:>java helloclient
闽公网安备 35060202000074号