The client establishes a connection to the server every 8 seconds, and closes the connection after the content is sent;
The server continuously accepts client connection requests and prints the received content;
The client reads the content from the local file and sends it to the client;
Client code: client_main.cpp
# Include <iostream> # include <string> using namespace STD; # include <fstream> # include <iterator> # include "ACE/inet_addr.h" # include "ACE/sock_connector.h" # include "ACE/OS. H "# include" ACE/log_msg.h "# include" ACE/event_handler.h "# include" ACE/reactor. H "# include" ACE/thread_manager.h "# include" ACE/inet_addr.h "# include" ACE/sock_stream.h "class my_timer_handler: Public ace_event_handler {public: my_timer_ha Ndler (const int delay, const int interval, const ace_inet_addr & ADDR );~ My_timer_handler (); int handle_timeout (const ace_time_value &, const void * Act/* = 0 */); // callback function executed after the timer expires PRIVATE: void start_timing (void ); bool send_file (void); long time_handle _; // ID int delay _ In the timer queue; int interval _; ace_inet_addr ADDR _; // ace_inet_addr (9997, ace_localhost ); ace_sock_connector conne_; ace_sock_stream peer _;}; my_timer_handler: my_timer_handler (const int delay, const int I Nterval, const ace_inet_addr & ADDR): delay _ (Delay), interval _ (interval), ADDR _ (ADDR) {cout <"my_timer_handler ()" <Endl; this-> Reactor (ace_reactor: instance (); cout <"start_timing [" <this-> delay _ <", "<this-> interval _ <"] "<Endl; // The task is triggered by the timer start_timing ();} void my_timer_handler: start_timing (void) {This-> time_handle _ = This-> Reactor ()-> schedule_timer (this, // register the timer 0, ace_time_value (this-> delay _), // The program starts execution of the expiration function for the first time with a delay of delay seconds. The ace_time_value (this-> interval _); // repeats execution every interval seconds.} int my_timer_handler :: handle_timeout (const ace_time_value &, const void * Act/* = 0 */) {cout <"\ n timer expires, start searching for the file and try to send it to the peer "<Endl; char arr [200] = {0}; this-> ADDR _. addr_to_string (ARR, sizeof (ARR); cout <"TCP starts to establish a connection with the peer end:" <arr <Endl; If (this-> connector _. connect (this-> peer _, this-> ADDR _) =-1) {cout <"TCP connection peer failed" <Endl;} Else {cout <"start sending files to the peer end" <Endl; If (send_file () {cout <"TCP Sending File Content succeeded" <Endl; cout <"close connection" <Endl; this-> peer _. close () ;}} return 0 ;}bool my_timer_handler: send_file (void) {string file_name ("temp_file.xml"); ifstream fin (file_name ); istream_iterator <char> iter_begin (FIN), iter_end; string send_str (iter_begin, iter_end); cout <"send" <send_str.size () <"bytes \ n sent content: "<Endl <send_str <Endl; int send_leng Th = This-> peer _. send_n (send_str.c_str (), send_str.size (); If (send_length =-1) {cout <"TCP Sending File Content failed" <Endl; return false ;} return true;} my_timer_handler ::~ My_timer_handler () {cout <"~ My_timer_handler () "<Endl; cout <" cancel_timer (this-> time_handle _) "<Endl; ace_reactor: instance () -> cancel_timer (this-> time_handle _);} int main (INT argc, char * argv []) {int delay = 2; int interval = 8; // incluaddr ("Clerk: 9997"); ace_inet_addr server_addr (9997, ace_localhost); my_timer_handler my_handle (delay, interval, server_addr); While (true) {ace_reactor :: instance ()-> handle_events ();} return 0 ;}
Server code: server_main.cpp
# Include "ACE/inet_addr.h" # include "ACE/time_value.h" # include "ACE/sock_acceptor.h" # include "ACE/sock_stream.h" # include "ACE/log_msg.h" # include "ACE/ OS. H "# include <string >#include <iostream> using namespace STD; int main (INT argc, char * argv []) {ace_inet_addr port_to_listen (9997); ace_sock_stream peer; ace_sock_acceptor acceptor; If (acceptor. open (port_to_listen, 1) =-1) {ace_error_return (lm_error, ace_text ("% P \ n"), ace_text ("acceptor. open "), 1);} else {cout <" Start listen "<Endl;} ace_inet_addr peer_addr; char buffer [1024*60] = {0 }; ssize_t bytes_received; while (true) {If (acceptor. accept (peer, & peer_addr) =-1) {cout <"failed to accept connection" <Endl ;}else {cout <"connection setup" <Endl; const int name_length = 1000; char peer_name [name_length] = {0}; peer_addr.addr_to_string (peer_name, name_length); cout <"connect from" <peer_name <Endl; bytes_received = peer. recv (buffer, sizeof (buffer); If (bytes_received =-1) {cout <"socket not exist" <Endl; peer. close ();} else if (bytes_received = 0) {cout <"socket closed" <Endl; peer. close () ;}else {string STR (buffer, bytes_received); cout <"received message length:" <bytes_received <Endl <"received content: \ n "<STR <Endl <" \ n ";} ace_time_value T (); ace_ OS: Sleep (t );}} return 0 ;}
Output:
Ace basic TCP/IP socket usage-TCP communication (print what is received) 1.0