ACE基本的TCP通信(定时发送,实时显示)1.0

来源:互联网 发布:mac浏览器开发者模式 编辑:程序博客网 时间:2024/06/07 00:50

客户端每隔8秒钟向服务端建立连接,发送内容之后关闭连接;

服务端不停的接受客户端的连接请求,收到内容之后就打印出来;

客户端从本地文件中读取内容发送给客户端;

 

客户端代码: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_Handler(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 */);//计时器到期后执行的回调函数private:    void start_timing(void);    bool send_file(void);    long time_handle_;//在计时器队列中的ID    int delay_;    int interval_;    ACE_INET_Addr addr_;    //    ACE_INET_Addr addr(9997,ACE_LOCALHOST);    ACE_SOCK_Connector connector_;    ACE_SOCK_Stream peer_;};My_Timer_Handler::My_Timer_Handler(const int delay,    const int interval,    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;    //任务均由计时器触发    start_timing();}void My_Timer_Handler::start_timing(void){    this->time_handle_ = this->reactor()->schedule_timer(this,//在这里注册定时器        0,        ACE_Time_Value(this->delay_),//程序一开始延迟delay秒开始首次执行到期函数        ACE_Time_Value(this->interval_));//循环计时,每隔interval秒重复执行}int My_Timer_Handler::handle_timeout(const ACE_Time_Value& , const void *act /* = 0 */){    cout<<"\n\n\n计时器到期,开始查找文件并尝试发送到对端"<<endl;    char arr[200]={0};    this->addr_.addr_to_string(arr,sizeof(arr));    cout<<"TCP开始和对端建立连接:"<<arr<<endl;    if (this->connector_.connect(this->peer_,this->addr_) == -1)    {        cout<<"TCP 连接对端失败"<<endl;    }    else    {        cout<<"开始发送文件到对端"<<endl;        if (send_file())        {            cout<<"TCP发送文件内容成功"<<endl;            cout<<"关闭连接"<<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_str.size()<<"个字节\n发送内容:"<<endl<<send_str<<endl;    int send_length = this->peer_.send_n(send_str.c_str(),send_str.size());     if (send_length == -1)    {        cout<<"TCP发送文件内容失败"<<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;    //ACE_INET_Addr addr("221.226.0.182: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_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<<"接受连接失败"<<endl;        }        else        {            cout<<"连接建立"<<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<<"收到的报文长度: "<<bytes_received<<endl                    <<"收到的内容: \n"<<str<<endl<<"\n\n\n";            }            ACE_Time_Value t(0,1000);            ACE_OS::sleep(t);        }    }    return 0;}

输出:


0 0
原创粉丝点击