Ice topic: Practical distributed Hello word [original]

Source: Internet
Author: User

For basic ice knowledge, refer to the previous ice topic article. Because these articles are from the Internet, they are not posted on the homepage. The link is provided below:

Ice topic: notes on using ice to write programs

Ice topic: Ice Architecture

Ice topic: Starting from ice

Ice topic: Five services of ice

Ice topic: use C # to compile an ice Distributed Application

Ice topic: Learn about ice 3.0

Ice topic: how to locate the server on the client (that is, how to find a proxy)

Ice: how to install ice in Linux

Ice topic: Introduction to ice

 

Objective: To compile the first ice-based Hello Word program on Windows.

1. Installation

Download the installation file from http://www.zeroc.com/download.html?bin=:

Windows Installer for Visual Studio 2005

Ice-3.2.0-VC80.msi (required]

Everything needed to use ice on Windows XP/Server 2003/Vista in C ++, Java, C #, Visual Basic and python. this installer implements des executables, debug and release DLLs, header files, import libraries, PDBs, Java classes, sample programs, and third-party dependencies.

Visual Studio 2005 is only required for C ++, C #, and Visual Basic developers. Visual Studio 2005 express compilers are also supported.

Ice-3.2.0-ThirdParty-VC80.msi

Use this installer if you want to build ice for C ++ or ice for Java from sources. it supports des header files, debug/release DLLs, and jar files for ICE's third-party dependencies:

  • Berkeley dB 4.5.20
  • OpenSSL 0.9.8d
  • Libbzip2 1.0.3
  • Expat 2.0.0

Ice for Java users who do not want to install the full ice distribution may also find this installer useful, as it includes des the Java classes for Berkeley dB.

 

Ice for C #

IceCS-3.2.0.tar.gz
IceCS-3.2.0.zip

The complete ice for C # source code.

Note that, to build ice for C #, you must first compile ice for C ++ or, alternatively, download and install an appropriate binary distribution. this is necessary because the slice-to-C # compiler is written in C ++.

 

The above three files, Ice-x.x.-vc80.msi is the ice runtime file in windows, contains the necessary library files, header files, and command line tools. For an introduction to runtime, we will introduce the topic articles in the future one by one (with the project progress gradually introduced: P ). It is recommended to install the 3rd library, especially during C ++ development. Of course, if you do not use C ++, you do not have to install it. The ice for C # package is a source code library that contains the implementation of ice for C #, which is actually encapsulated. net Framework and ice Runtime Library, that is. net program and C ++ program interaction, you will see a large number of Platform calls. For a better name, it may be called "ice for C # provider/wrapper.

Note that after installation and running, you need to add the environment variable ice_home and add % ice_home % \ bin to path:

2. Create a C # console Project

Start VS 2005 and create two console projects, server and client. Add references to icecs. dll.

Because my ice is installed in c: \ ice-3.2.0 during running, find this file in c: \ ice-3.2.0 \ bin. Note that you must select: copy to local.

3. Define interface methods

module Demo {interface Printer {void printString(string s);};};

 

Save this code as printer. Ice. In the same directory as this file, create a batch processing command file named commit 1.bat with the following content:

mkdir generatedslice2cs --output-dir generated Printer.ice

Run export 1.bat to generate printer. Cs in the generated folder. We will not analyze the structure of printer. CS for the moment, so we will use it directly. Copy printer. CS to the two new projects. Note that you must set the namespace used in the project to the same one. For example, I set the root namespace to demo.

 

4. Add application logic

Add client. CS to the client project. The content is as follows:

using System;using System.Collections.Generic;using System.Text;namespace Demo{public class Client{public static void Main(string[] args){int status = 0;Ice.Communicator ic = null;try{ic = Ice.Util.initialize(ref args);Ice.ObjectPrx obj = ic.stringToProxy("SimplePrinter:tcp -p 12345:udp -p 12345");PrinterPrx printer= PrinterPrxHelper.checkedCast(obj);if (printer == null)throw new ApplicationException("Invalid proxy");printer.printString("Hello World!");}catch (Exception e){Console.Error.WriteLine(e);status = 1;}if (ic != null){// Clean up//try{ic.destroy();}catch (Exception e){Console.Error.WriteLine(e);status = 1;}}Environment.Exit(status);}}}

 

Add the server. CS file in the server project. The content is as follows:

namespace Demo{using System;public class PrinterI : Demo.PrinterDisp_{public override void printString(string s, Ice.Current current){Console.WriteLine(s);}}public class Server{public static void Main(string[] args){int status = 0;Ice.Communicator ic = null;try{ic = Ice.Util.initialize(ref args);Ice.ObjectAdapter adapter= ic.createObjectAdapterWithEndpoints("SimplePrinter", "tcp -p 12345:udp -p 12345");Ice.Object obj = new PrinterI();adapter.add(obj,Ice.Util.stringToIdentity("SimplePrinter"));adapter.activate();ic.waitForShutdown();}catch (Exception e){Console.Error.WriteLine(e);status = 1;}if (ic != null){// Clean up//try{ic.destroy();}catch (Exception e){Console.Error.WriteLine(e);status = 1;}}Environment.Exit(status);}}}

5. test applications

Switch to the server object directory and find that the server.exe program is generated and run: server.exe.
If your OS is installed with a network firewall, activate the corresponding port 12345. It is best to test the application after shutting down all firewall software.
After server.exe runs, it remains in the listening state. Run netstat-An. You can see that the server-side program listens to port 12345.

Run client.exe on the command line and you will find that "Hello word!" is displayed on the console of server.exe!

 

 

Note:

1. Use TCP or UDP as the communication protocol string. as provided above, the protocol in the ice manual is: "default-P 10000 ", this is not available on my platform. You need to configure the default settings for ice. The "program name. config" file is used to configure the ice settings. The following is an example:

#
# The client reads this property to create the reference to
# "Hello" object in the server.
#
Hello. Proxy = Hello: TCP-P 10000: UDP-P 10000: SSL-P 10001

#
# Warn about connection exceptions.
#
# Ice. Warn. Connections = 1

#
# We want a faster ACM for this demo.
#
Ice. ACM. Client = 10

#
# Network Tracing
#
#0 = No network Tracing
#1 = trace connection establishment and Closure
#2 = like 1, but more detailed
#3 = like 2, but also trace data transfer
#
# Ice. Trace. Network = 1

#
# Protocol tracing
#
#0 = No protocol tracing
#1 = trace protocol messages
#
# Ice. Trace. Protocol = 1

#
# Security Tracing
#
#0 = No security Tracing
#1 = trace messages
#
# Icessl. Trace. Security = 1

#
# SSL Configuration
#
Ice. plugin. icessl = icesslcs, version = 3.2.0.0, culture = neutral, publickeytoken = 1f998c50fec78381: icessl. pluginfactory
Icessl. defaultdir =.../certs
Icessl. importcert. currentuser. Root = cacert. pem
Icessl. certfile = c_rsa1024.pfx
Icessl. Password = Password
Ice. threadperconnection = 1

2. Close the Protection Wall
When the firewall is not disabled, some inexplicable problems occur, all of which are caused by restrictions on the TCP/IP access of the program.

Postscript:

Recently, the project used ice to implement distributed applications. Ice is indeed very powerful. We hope that friends who have used it or who are interested in ice can exchange experiences and improve each other. [Open Source Application Technology Group 25935569 ].

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.