This is an example in Chapter 6 of the ACE learning tutorial mentioned in the title.
# Include "ACE/reactor. H "# include" ACE/handler "# define port_no 19998 typedef ace_sock_acceptor acceptor; // Forward Declaration class my_accept_handler; // data processor class my_input_handler: Public ace_event_handler {public: // constructor my_input_handler () {ace_debug (lm_debug, "constructor \ n");} // calls back any data from the handle int handle_input (ace_handle) {// obtain the data peer_ I (). recv_n (data, 12); ace_debug (lm_debug, "% s \ n", data); // return 0 in the event processing area;} // required, ace_handle get_handle (void) {return this-> peer_ I (). get_handle ();} ace_sock_stream & peer_ I () {return this-> peer _;} PRIVATE: ace_sock_stream peer _; // event processing I/O entry, buffer char data [12] ;}; // accept the processor class my_accept_handler: Public ace_event_handler {public: // construct my_accept_handler (ace_addr & ADDR) {This-> open (ADDR );} // start listening // accept the request int open (ace_addr & ADDR) {peer_acceptor.open (ADDR); Return 0 ;}// reload the I/O processing area from the client, client to process int handle_input (ace_handle handle) {// a client has been created to create a data processor instance to process my_input_handler * Eh = new my_input_handler (); // accept the connection "into" the event handler if (this-> peer_acceptor.accept (Eh-> peer_ I (), // Stream object 0, // remote address 0, // timeout 1) =-1) // restart if interrupted ace_debug (lm_error, "error in connection \ n"); ace_debug (lm_debug, "Connection established \ n"); // bind the processor to the ace_reactor: instance ()-> register_handler (EH, ace_event_handler: read_mask ); // if you want to process more clients, return-1. If you want to permanently multiple clients, return 0 return 0;} // required, ace_handle get_handle (void) const {return this-> peer_acceptor.get_handle ();} PRIVATE: acceptor peer_acceptor;}; int main (INT argc, char * argv []) {incluaddr (port_no); inclu* Eh = new my_accept_handler (ADDR); ace_reactor: instance ()-> register_handler (EH, ace_event_handler: accept_mask); While (1) ace_reactor: instance ()-> handle_events ();}
Post: http://raojl.iteye.com/blog/337952