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:
Write an slice definition and compile it
Write the server and compile it.
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.
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.
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
-
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 ?)
-
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)
-
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
-
modify printer. # include # include "printer. H "
-
OK, compile
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)