复制代码
- moduleRemoteTime{
- interfaceExactTime{
- stringgetTime();
- };
- };
复制代码
- importRemoteTime.*;
- importorg.omg.CosNaming.*;
- importorg.omg.CosNaming.NamingContextPackage.*;
- importorg.omg.CORBA.*;
- importjava.util.*;
- importjava.text.*;
- //Serverobjectimplementation
- classExactTimeServerextends_ExactTimeImplBase{
- publicStringgetTime(){
- returnDateFormat.
- getTimeInstance(DateFormat.FULL).
- format(newDate(
- System.currentTimeMillis()));
- }
- }
- //Remoteapplicationimplementation
- publicclassRemoteTimeServer{
- publicstaticvoidmain(Stringargs[]){
- try{
- //ORBcreationandinitialization:
- ORBorb=ORB.init(args,null);
- //Createtheserverobjectandregisterit:
- ExactTimeServertimeServerObjRef=
- newExactTimeServer();
- orb.connect(timeServerObjRef);
- //Gettherootnamingcontext:
- org.omg.CORBA.ObjectobjRef=
- orb.resolve_initial_references(
- "NameService");
- NamingContextncRef=
- NamingContextHelper.narrow(objRef);
- //Assignastringnametothe
- //objectreference(binding):
- NameComponentnc=
- newNameComponent("ExactTime","");
- NameComponentpath[]={nc};
- ncRef.rebind(path,timeServerObjRef);
- //Waitforclientrequests:
- java.lang.Objectsync=
- newjava.lang.Object();
- synchronized(sync){
- sync.wait();
- }
- }
- catch(Exceptione){
- System.out.println(
- "RemoteTimeservererror:"+e);
- e.printStackTrace(System.out);
- }
- }
- }
复制代码
- importRemoteTime.*;
- importorg.omg.CosNaming.*;
- importorg.omg.CORBA.*;
- publicclassRemoteTimeClient{
- publicstaticvoidmain(Stringargs[]){
- try{
- //ORBcreationandinitialization:
- ORBorb=ORB.init(args,null);
- //Gettherootnamingcontext:
- org.omg.CORBA.ObjectobjRef=
- orb.resolve_initial_references(
- "NameService");
- NamingContextncRef=
- NamingContextHelper.narrow(objRef);
- //Get(resolve)thestringifiedobject
- //referenceforthetimeserver:
- NameComponentnc=
- newNameComponent("ExactTime","");
- NameComponentpath[]={nc};
- ExactTimetimeObjRef=
- ExactTimeHelper.narrow(
- ncRef.resolve(path));
- //Makerequeststotheserverobject:
- StringexactTime=timeObjRef.getTime();
- System.out.println(exactTime);
- }catch(Exceptione){
- System.out.println(
- "RemoteTimeservererror:"+e);
- e.printStackTrace(System.out);
- }
- }
- }
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |