Internet communications engine (ICE) is an object-oriented middleware platform mainly used for network communication. It provides a set of good tools and APIs for Object-Oriented "client-server" models. Currently, it has been applied to many projects around the world. Ice middleware is known as standard and unified, open-source, cross-platform, cross-language, distributed, secure, service transparent, load balancing, object-oriented, superior performance, penetration during fire prevention, communication shielding. Therefore, compared with middleware technologies such as CORBA, DCOM, soap, and J2EE, it is natural to integrate many advantages without their disadvantages.
The main process of this write is very simple, that is, the client sends a message to the server and the server prints it out and displays it on the screen. It can also be said that it is a program similar to a small printer.
Hello world application:
1. The first step in writing any ice application is to write an slice definition, which contains
Interfaces used. We have written such a slice definition for our small print application:
We saved the text in a file named printer. Ice.
Our slice definition contains an interface called printer. Currently, our interface is very simple and only provides one operation, called printstring. The printstring operation accepts a string as its unique input parameter. The text of this string will appear on (possibly far away) the printer.
2. WriteC #OfSliceDefinition:
To create our C # application, the first step is to compile our slice definition and generateC #Proxy and skeleton. On Windows, you can compile the definition as follows:
Slice2cs.exe printer. Ice
Slice2csThe compiler generates some. Cs source files based on this definition. At present, we do not need to pay attention to the exact content of these files-they contain the code generated by the compiler, which corresponds to the printer interface we defined in printer. Ice.
Start --> Run --> cmd --> slice2cs.exe printer. Ice (remember to drag the ICE file you wrote into the corresponding folder you saw when running cmd) --> press ENTER
3. Compiling and compiling Server:
First, we need to add a reference to ice, right-click to add a reference, and select ice under the ice installation directory. DLL, for example, I am (c: \ Program Files \ zeroc \ ice-3.5.1 \ assemblies .. ice. DLL)
Second, to implement our printer interface, we must create a servant class. By convention,
The name of the servant class is their interface name plus an I suffix, so our servant
The class is called printeri and added to the project:
The printeri class inherits the base class called printerdisp. After inheriting the base class, we can simply right-click on printerdisp _ to implement the abstract class, the function will be automatically overwritten in this base class (in fact, the interface we defined in the ice file ). This base class consistsSlice2csThe compiler generates an abstract class containing a printstring method. The parameter is the string to be printed by the printer and the type is ice. current object. the implementation of our printstring method will simply write its parameters to the terminal.
The rest of the server code is in a source file called server. cs. The complete code is provided below:
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 6 namespace Server 7 {8 class program 9 {10 static void main (string [] ARGs) 11 {12 INT status = 0; 13 14 // create a communicator 15 ice. communicator Ic = NULL; 16 try17 {18 // 19 Ic = ice during initialization. util. initialize (ref ARGs); 20 21 // create an adapter object. The parameter is (adapter name, default TCP/IP, Port Number) 22 ice. objectadapter adapter = IC. createobjectadapterwithenderson points ("Simpleprinter", "default-P 10000"); 23 24 // The Run Time on the server has been initialized, and then the printeri is instantiated and assigned to the interface (to obtain an object that can complete the specific method) 25 // printeri P = new printeri (); ice. object o = P; 26 ice. object OBJ = new printeri (); 27 28 // input the object in the adapter, and the adapter Object Name (the recipient may have multiple) 29 adapter. add (OBJ, ice. util. stringtoidentity ("simpleprinter"); 30 31 // activate the adapter (the adapter is in the holding state after it is created) 32 adapter. activate (); 33 34 // suspends the thread that sends the call until the server terminates 35 IC. waitforshutdown (); 36} 37 C Atch (exception e) 38 {39 console. Error. writeline (E); 40 status = 1; 41} 42 finally43 {44 If (IC! = NULL) 45 {46 IC. destroy (); 47} 48} 49 50 // terminate the process and provide the specified exit code 51 environment for the basic operating system. exit (Status); 52} 53} 54}
The main body contains a try block. We put all the server code in it; then there are two catch processors. The first processor captures all the exceptions that the ice run time may throw. The intention is that if the code encounters an unexpected ice runtime exception anywhere, the stack will always return to main, print out the exception and return the failure to the operating system. The second processor captures string constants. The intention is that if we encounter a fatal error somewhere in the code, we can simply throw the string text with an error message. This will also cause the stack to return to main, print an error message, and then return the failure to the operating system.
This code will destroy the communicator before exiting (if it has been successfully created ). To make the ice run time end normally, this is required: The program must call the destroy of any communication device it creates; otherwise, it will produce uncertain behavior. We put the call to destroy into the Finally block. In this way, no matter what exception occurs in the previous try block, the communicator will ensure correct destruction.
4. Write and compile the client
The Customer Code in client. CS looks very similar to the server. Similarly, you need to upload the class file generated by slice2cs.exe printer. ice to the project and add a reference to ice. dll. In this case, you do not need to use methods in the override base class. In fact, it is very simple because we want the client to send an instruction to the server and let the server complete it. Below is the complete
Code:
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using family; 6 7 namespace iceclient 8 {9 class program10 {11 static void main (string [] ARGs) 12 {13 int status = 0; 14 15 // create a communicator 16 ice. communicator Ic = NULL; 17 try18 {19 // 20 Ic = ice during initialization. util. initialize (ref ARGs); 21 22 // call the stringtoproxy of the communicator to create a proxy (this string contains the Object ID and the port number used by the server) 23 ice. objectprx OBJ = IC. stri Ngtoproxy ("simpleprinter: default-P 10000"); 24 25 // obtain the printer interface remotely, and check whether the service unit obtained based on the input name is a printer proxy interface, if not, null object 26 printerprx printer = printerprxhelper is returned. checkedcast (OBJ); 27 if (printer = NULL) 28 {29 throw new applicationexception ("invalid proxy"); 30} 31 printer. printstring ("Hello world! "); 32} 33 catch (exception e) 34 {35 console. Error. writeline (E); 36 Status = 1; 37} 38 finally39 {40 if (IC! = NULL) 41 IC. Destroy (); 42} 43 environment. Exit (Status); 44} 45} 46}
The result is as follows: