JAVA client-server communication implementation

Source: Internet
Author: User

IDE: Eclipse 3.4.1

Create two Java projects in eclipse

1.Then define the IDL.

 

The content is as follows:

 

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

 

 

 

** Note here,} The ";" must be written, otherwise compilation errors will occur.

 

2.Right-click HelloApp. idlFile

Click, change to parent folder in Shell

Click OK. eclipse will open the shell view, which is a command line view. In fact, it works with Cmd.

Run the following command:Idlj-fall HellApp. idl

 

3.Then go to the project directory (H: \ DEVELOPING \ UMM \ workspace \ testCorbaServer \ src \ server)You can see that a folder named helloapp is created.

Open to interview some java interface classes

4.Create a package on the serverName and idlModule in the contentSame,Here we are'Helloapp'

Then copy the class to the package.

 

 

---------------------------------------

---------------------------------------

Part 2Implement IDLInterface of the generated class

1.Implement Server

The content is as follows:

package server;import org.omg.CORBA.ORB;import helloapp.HelloPOA;public class HelloImpl extends HelloPOA {    private ORB orb;    public void setOrb(ORB orb) {        this.orb = orb;    }    @Override    public String sayHello() {        return "\nHello World!!\n";    }    @Override    public void shutdown() {        orb.shutdown(false);    }}

 

Above,"Service"Write it,Next, we need to write an entry class on the server,Put your service"Register".

 

The content is as follows:

 

Package server; import helloapp. hello; import helloapp. helloHelper; import org. omg. CORBA. ORB; import org. omg. CORBA. ORBPackage. invalidName; import org. omg. cosNaming. nameComponent; import org. omg. cosNaming. namingContextExt; import org. omg. cosNaming. namingContextExtHelper; import org. omg. cosNaming. namingContextPackage. cannotProceed; import org. omg. cosNaming. namingContextPackage. notFound; import org. omg. portableServer. POA; import org. omg. portableServer. POAHelper; import org. omg. portableServer. POAManagerPackage. adapterInactive; import org. omg. portableServer. POAPackage. servantNotActive; import org. omg. portableServer. POAPackage. wrongPolicy; public class Main {public static void main (String [] args) {try {// create an ORB instance ORB orb = ORB. init (args, null); // get a reference to RootPOA and activate POAManager POA rootpoa = POAHelper. narrow (orb. resolve_initial_references ("RootPOA"); rootpoa. the_POAManager (). activate (); // create servant and register it with ORB // create a HelloImpl instance (servant) and register it with ORB HelloImpl helloImpl = new HelloImpl (); helloImpl. setOrb (orb); // get the reference org of the object in the service. omg. CORBA. object ref = rootpoa. servant_to_reference (helloImpl); Hello href = HelloHelper. narrow (ref); // get the context org of a root name. 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 this object String name = "Hello"; NameComponent path [] = ncRef in the naming context. to_name (name); ncRef. rebind (path, href); System. out. println ("HelloServer ready and waiting... "); // start the thread service and wait for the client to call orb. run ();} catch (InvalidName e) {e. printStackTrace ();} catch (AdapterInactive e) {e. printStackTrace ();} catch (ServantNotActive e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (WrongPolicy e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (org. omg. cosNaming. namingContextPackage. invalidName e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (NotFound e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (CannotProceed e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println ("HelloServer Exiting... ");}}

 

 

 

Note: although this is an entry, you cannot right-click Run as-> JAVA application

 

 

---------------------------------------

 

---------------------------------------

 

 Implement Client

1.Copy the tool class generated by the server to the client (It is recommended that the package structure be consistent)

2.Write client calls.

The content is as follows:

Package client; import helloapp. hello; import helloapp. helloHelper; import org. omg. CORBA. ORB; import org. omg. cosNaming. namingContextExt; import org. omg. cosNaming. namingContextExtHelper; public class HelloClient {static Hello helloImpl; public static void main (String [] args) {try {// create an ORB instance ORB orb = ORB. init (args, null); // get the root name context org. omg. CORBA. object objRef = orb. resolve_initial_references ("NameService"); // Use NamingContextExt instead of NamingContext. this is // part of the Interoperable naming Service. namingContextExt ncRef = NamingContextExtHelper. narrow (objRef); // obtain the interface implementation object from the context String name = "Hello"; helloImpl = HelloHelper. narrow (ncRef. resolve_str (name); System. out. println (helloImpl. sayHello (); System. out. println ("asdfasdfdasf");} catch (Exception e) {e. printStackTrace ();}}}

 

In this way, the client completes the call, and the following describes how to run it.

 

1.Run CORBAService (CMDRun the command)

Orbd-orbinitialport 1053-orbinitialhost192.168.1.42

Note:192.168.1.42It's my local IP address. 1053 is the port.

CORBAThe service is independent of the client and server, which is equivalent to a bridge.

 

Do not close this window, And the cursor will flash.

 

2.Start the server

To start the server, the main function must pass parameters. Therefore, we need to configure the main function parameters first .:

 

 

Click to open:

 

Main is the entry function of our server.

 

Then, in the program arguments of arguments: Write

 

-ORBInitialPort1053

 

 

 

Port 1053Is different from the previousCorresponding to the Service port

 

 

 

The server is configured. Run the server.

 

Do not close the server.

 

3.Start the client

 

I don't need to talk about it here, and then start the running, you can see the running result.

 

Note: according to the original article, there may be some strange problems in different environments. If you want to carefully check whether there is any discrepancy with the original article, or check whether the problem is abnormal or not, leave a message to discuss it.

It is indeed troublesome to establish the client-server of CORBA. However, if you have already set up a framework, the development process will get twice the result with half the effort.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.