码迷,mamicode.com
首页 > 其他好文 > 详细

ACE基本的TCP/IP Socket用法之TCP通信(接收什么就打印什么)1.0

时间:2014-09-19 17:39:35      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   文件   2014   art   sp   

客户端每隔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;
}

输出:

bubuko.com,布布扣

ACE基本的TCP/IP Socket用法之TCP通信(接收什么就打印什么)1.0

标签:blog   http   io   os   ar   文件   2014   art   sp   

原文地址:http://blog.csdn.net/calmreason/article/details/39400533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!