below is a simple example of a corba program
download the source file
1. produce a idl file like this
hello.idl
module helloapp {
interface hello {
string sayhello();
};
};
2. produce stub and skeleton files through idltojava.exe
idltojava hello.idl
idltojava is not include in the jdk. but you can download it from idldojava.
3. write a server program like this
// 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 "\nhello world !!\n";
}
}
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[] = {nc};
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);
}
}
}
4. write a client program like this
// 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[] = {nc};
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);
}
}
}
5. complie these files
javac *.java helloapp/*.java
6. run the application
a. first youve to run the name service prior to the others likethis
c:\>tnameserv
b. run server
c:\>java helloserver
c. run client
c:\>java helloclient
if you have problems with this example, do let me know!
