ICE Learning Notes

Source: Internet
Author: User
Hello.ice #ifndef Simple_ice
#define Simple_ice
Name space or package name
Module Demo.slice.Hello
{
Interface hello{
void Printstring (string s);
};
}; #endif
Server:
Helloi.java
Package demo.ice.Hello.server;
Import Demo.slice.hello._hellodisp;
Import ice.current;
/**
* @author MUDFISHCN
*
* Servant Class:helloi
* The _HELLODISP base class is generated by the Slice2java compiler, which is an abstract class.
* _hellodisp abstract class contains a printstring () method.
*/
public class Helloi extends _hellodisp {
/* (Non-javadoc)
* @see demo._hellooperations#printstring (java.lang.String, ice.current)
*/
public void printstring (String s, current __current) {
TODO auto-generated Method Stub
System.out.println (s);
}
}
Helloserver.java
Package demo.ice.Hello.server;
Import ice.localexception;
/**
* @author MUDFISHCN
* version:1.0.1
*/
public class HelloServer {
public static void Main (string[] args) {
Defines the status variable, which is used to control the server.
int status = 0;
Ice.communicator IC = null; Declare a Communicator object IC
try {
Initializing the Ice runtime
IC = Ice.Util.initialize (args);
/*
* Create an Object adapter (Objectadapter) object Ioadapter and initialize it.
* parameter "Helloworldadapter": Represents the name of the adapter.
* parameter "default-p 10000": Indicates that the adapter uses the default protocol (TCP/IP) to listen for incoming requests at Port 10000.
* Server configuration complete.
*/
Ice.objectadapter Ioadapter = ic.createobjectadapterwithendpoints (
"Helloworldadapter", "default-p 10000");
/*
* Create a servant for the Hello interface.
*/
Ice.object Iobject = (ice.object) new Helloi ();
  
/*
* Add the new servant to the adapter,
* and named this new servant "HelloWorld."
*/
Ioadapter.add (Iobject, Ice.Util.stringToIdentity ("HelloWorld"));
  
/*
* Activate the adapter to enable the server to start processing requests from customers
*/
Ioadapter.activate ();
/*
* Suspends the calling thread until the server implementation terminates.
* or terminate the server by issuing an instruction that shuts down the runtime (run time).
*/
Ic.waitforshutdown ();
catch (Localexception e) {
Catch all exceptions that may be thrown when the ice is running
E.printstacktrace ();
status = 1;
catch (Exception e) {
Catch string constants
E.printstacktrace ();
status = 1;
finally {
if (IC!= null) {
Ic.destroy ();
}
}
System.exit (status);
}
}
Helloclient.java
Package demo.ice.Hello.client;
Import Ice.objectprx;
Import Ice.util;
Import Demo.slice.Hello.HelloPrx;
Import Demo.slice.Hello.HelloPrxHelper;
/**
* @author MUDFISHCN
*/
public class Helloclient {
public static void Main (string[] args) {
int status = 0;
String s;
To create an object IC for a communicator
Ice.communicator IC = null;
try {
Initializing the Ice runtime
IC = util.initialize (args);
  
/*
* Get proxy for remote object
* Create a proxy object and initialize it with the Stringtoproxy () method of the Communicator.
* Parameter: "Helloworld:default-p 10000"
Said
*/
Objectprx base = Ic.stringtoproxy ("helloworld:default-p 10000");
/*
* Key points
*/
Helloprx Hello = helloprxhelper.checkedcast (base);
Test whether the downward conversion was successful. If the exception message "Invalid Proxy" is not successfully thrown.
if (hello = = null) {
throw new Error ("Invalid proxy");
}
Send 10 messages to the server
for (int i=1;i<=10;i++) {
s = "hello,world!" +integer.tostring (i);
Hello.printstring (s);
}
catch (Ice.localexception e) {
E.printstacktrace ();
status = 1;
catch (Exception e) {
E.printstacktrace ();
status = 1;
finally {
if (IC!= null) {
Destroy a communication connection
Ic.destroy ();
}
}
System.exit (status);
}
}

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.