Learn about ice 3.0-preparations

Source: Internet
Author: User

According to someone, cross-platform C ++ network programming ice is king. So I learned ice.

Two years ago, ice was "a modern object-oriented middleware that can be used to replace middleware such as CORBA or COM/DCOM/COM +. While being easy to learn, it provides powerful network infrastructure for a variety of applications with demanding technical requirements ." Ice
3.0 supports C ++, Java, Python, PHP, C #, and Visual Basic.

I will not talk about it here. You can refer to this article.Article: Ice of rebellion: Internet communications Engine
. You can download the official reference manual of ice, which has a Chinese version, but is 1.3.0 and 3.0 in English.

Ice is open-source.Source codeCompilation started, but it was complicated. Fortunately, there was a binary version. For example, I downloaded the vs2003.net installation package. After the installation is complete, configure the IDE according to readme in the installation directory. For example, vc7.1 adds the include of ice to the reference file directory of vc7.1, add the lib directory of ice to the library file directory of vc7.1. Then add the bin folder under the installation directory to the path of the system's environment variable. Finally, copy all DLL files in the bin folder to the System32 folder in the Windows Installation Directory (which is the system folder in Win98 ?).

Ice is a custom Server Load balancer language designed to define interfaces. It is mainly used to maintain language independence during object calls or data transmission.

Develop an ice ApplicationProgramThere are three steps:

    1. Write an slice definition and compile it

    2. Write the server and compile it.

    3. Write the client and compile it

OK, write a small program to send the text to be printed to the server, and then send the text to the printer by the server (Here we use screen display instead ).CodeFor more information, see the next chapter.

  1. Write an slice definition and compile it:

    File printer. Ice.

    Module demo {
    Interface printer {
    Void printstring (string S );
    };
    };

    This file is very simple, but note that the extension must be in lowercase on case-sensitive systems.

    Compilation is also very simple. first make sure that you have added your bin directory to the path of the system's environment variable. Then save the preceding part as printer. Ice, and finally execute slice2cpp.
    Printer. Ice, the result after execution should be automatically generated printer. h and printer. cpp.

  2. Write the server and compile it.

    File Server. cpp.

    # Include <ice/ice. h>
    # Include "../print. H"
    Using namespace STD;
    Using namespace demo;
    Class printeri: Public printer {
    Public:
    Virtual void printstring (const string & S, const ice: Current &);
    };
    Void printeri: printstring (const string & S, const ice: Current &)
    {
    Cout <S <Endl;
    }
    Int main (INT argc, char * argv [])
    {
    Int status = 0;
    Ice: communicatorptr IC;
    Try {
    Ic = ice: Initialize (argc, argv );
    Ice: objectadapterptr Adapter
    = IC-> createobjectadapterwithenderson points (
    "Simpleprinteradapter", "default-P 10000 ");
    Ice: objectptr object = new printeri;
    Adapter-> Add (object,
    Ice: stringtoidentity ("simpleprinter "));
    Adapter-> activate ();
    IC-> waitforshutdown ();
    } Catch (const ice: exception & E ){
    Cerr <e <Endl;
    Status = 1;
    } Catch (const char * MSG ){
    Cerr <MSG <Endl;
    Status = 1;
    }
    If (IC ){
    Try {
    IC-> destroy ();
    } Catch (const ice: exception & E ){
    Cerr <e <Endl;
    Status = 1;
    }
    }
    Return status;
    }

    Take vs2003 configuration as an Example

      1. Add the include of ice to the reference file directory of vc7.1, add the lib directory of ice to the library file directory of vc7.1. Then add the bin folder under the installation directory to the path of the system's environment variable. Finally, copy all DLL files in the bin folder to the System32 folder in the Windows Installation Directory (which is the system folder in Win98 ?) (Of course, the DLL file problem can also be solved by modifying the environment variable, but what is the variable? Who
        can tell me ?)

      2. Create a c ++ Win32 console control program and set it to an empty project, set server. CPP,
        printer. CPP and printer. h join this project (printer. CPP and printer. H is placed in the directory of the project)

      3. Project-" properties-"C/C ++-" code generation-"Runtime Library-"/MD (realse version) "or/MDD (debug version)

        Project-configure properties-C/C ++-language-enable runtime type information/GR enable

        Settings: Project-> properties-> linker-> input-> Add to iced. lib iceutild. lib. The realse and debug libraries must be distinguished here.
        the debug library has a d

      4. modify printer. # include # include "printer. H "

      5. OK, compile

  3. Write the client and compile it

    File client. cpp.

     
    # Include <ice/ice. h>
    # Include ".. \ Print. H"
    Using namespace STD;
    Using namespace demo;
    Int main (INT argc, char * argv [])
    {
    Int status = 0;
    Ice: communicatorptr IC;
    Try {
    Ic = ice: Initialize (argc, argv );
    Ice: objectprx base = IC-> stringtoproxy (
    & Quot; simpleprinter: default-P 10000 & quot ");
    Printerprx printer = printerprx: checkedcast (base );
    If (! Printer)
    Throw "invalid proxy ";
    Printer-> printstring ("Hello world! ");
    } Catch (const ice: exception & Ex ){
    Cerr <ex <Endl;
    Status = 1;
    } Catch (const char * MSG ){
    Cerr <MSG <Endl;
    Status = 1;
    }
    If (IC)
    IC-> destroy ();
    Return status;
    }

    Add a new project to the current solution, and set the client again according to the above method.

    Right-click solution in solution manager, select batch Generate debug version, and use resource manager to execute the production executable file in the debug folder under the two solution directories. Run server.exe first,
    Then run client.exe.
    World! (Run cmdclient.exe, one line appears)

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.