In previous technical articles on the use of ACL (for example, using ACL: master_threads To Write multi-process multi-thread server programs, using ACL: master_aio to write high-concurrency non-blocking server programs, using ACL: master_proc class to write a multi-process server program), describes in detail how to use the ACL Server framework class to write a server application, however, manual input of the Code is annoying after all. In the APP/directory of the ACL, there is a Wizard program: Wizard, which can help developers quickly generate Server framework templates, developers only need to add the business logic code in the relevant location. The following is a brief process of using the Wizard program:
First, compile the base database of all ACLs in the root directory of the ACL: Make all, and then go to the app/Wizard directory to generate the Wizard program: Make, run./Wizard. The following interface is displayed:
[[Email protected] wizard] $. /Wizard select one below: M: master_service; D: DB; H: HTTP; Q: exit # the above process prompts you which application to generate. Here we compile the server, so choose m> M #. Then, the wizard prompts us to enter the application name. You can enter echo_serverplease input your program name: echo_server #. Then, the wizard prompts you to select the Server framework template, here, "T" indicates that a multi-process and multi-thread server template is used. # parameter types are described as follows: # T: multi-process and multi-thread server template # P: multi-process server template #: multi-process non-blocking server template # G: trigger server template choose master_service type: T: For master_threads; P: For master_pr OC; A: For master_aio; G: For master_trigger; s: Skip choose> tcreate echo_server/makefile OK. create echo_server/echo_server.sln OK. create echo_server/echo_server.vcproj OK. create echo_server/makefile. in okcreate echo_server/stdafx. h okcreate echo_server/stdafx. CPP okcreate common_files OK! Create echo_server/echo_server.cf OK. Create echo_server/makefile. In okcreate echo_server/Main. cpp okcreate echo_server/master_service.h okcreate echo_server/master_service.cpp okcreate master_threads OK! ---------------------------------------------------------------------------- Select one below: M: master_service; D: DB; H: HTTP; Q: Exit> qbye!
OK. Enter a few letters and press Enter. a relatively complete server program is generated. From the above generation process, it is not difficult to see that the wizard not only generates the source file, but also the project file and the server configuration file.
Go to the echo_server directory, open the automatically generated master_service.cpp file, and modify the master_service: thread_on_read function content as follows:
Bool master_service: thread_on_read (ACL: socket_stream * conn) {ACL: String Buf; // read a row of data from the client connection stream. For more information, see: acl_cpp/stream/istream. HPP if (client-> gets (BUF, false) = false) {printf ("gets from client error, closed it! \ R \ n "); // returns false to notify the Server framework to disable the client connection and return false;} // read data write-back from the client connection stream. For write operations, see: acl_cpp/stream/ostream. HPP if (client-> write (BUF) =-1) {// return false to notify the Server framework to disable the client connection printf ("write to client error, closed it! \ R \ n "); Return false;} // return true to notify the Server framework to continue listening to the next request process connected to the client: Return true ;}
Modify the master_service: thread_on_accept function as follows:
Bool master_service: thread_on_accept (ACL: socket_stream * conn) {// set the client connection read/write timeout (seconds) Conn-> set_rw_timeout (10 ); // send the welcome message if (Conn-> Format ("+ OK Welcome! \ R \ n ") =-1) {printf (" write to client error, close it! \ R \ n "); // returns false to notify the Server framework to close the client connection and return false;} // returns true to notify the framework to monitor the readable status of the client stream and return true ;}
A very simple echo server program is completed, compiled (make), and then tested with the command line method :. /echo_server alone the server program prompts the following message: Listen on: 127.0.0.1: 8888, meaning that it is listening to port 8888 of the local loop address. You can manually test it by using the Telnet command:
[[email protected] ~]$ telnet 127.0.0.1 8888Trying 127.0.0.1...Connected to localhost.localdomain (127.0.0.1).Escape character is ‘^]‘.+OK Welcome!hello world!hello world!
Of course, in a formal production environment, you need to put the echo_server program under the acl_master Server framework. For the deployment method, see: deployment of the ACL Server Module-example, deployment of the ACL server module.
Refer:
ACL library download: https://sourceforge.net/projects/acl/
SVN: // svn.code.sf.net/p/acl/code/
QQ group: 242722074
Use the ACL generation Wizard to quickly create a server program