IMBFE
IMBFE
2019-06-11 10:59
采纳率: 100%
浏览 833

boost异步通信,async_read_some始终接收不到数据

我写了最简单boost异步通信例子,async_read_some始终接收不到数据。接收数据长度始终0,打印empty。甚至客户端断开连接后,仍打印empty,服务端对连接断开无反应。

boost版本为1_69。客户端代码,发送数据功能。服务端代码,async_read_some接收数据功能,如下:

class clients
{
public:
    clients(std::string str_address,int nport,int id_val)
        :m_ep(boost::asio::ip::address::from_string(str_address.c_str()), nport), _id(id_val)
    {
        start();
    }
    void run()
    {
        m_io.run();
    }
    void start()
    {
        boost::shared_ptr<boost::asio::ip::tcp::socket>sock(new boost::asio::ip::tcp::socket(m_io));
        sock->async_connect(m_ep,boost::bind(&clients::conn_handler,this,boost::asio::placeholders::error,sock));
    }
    void conn_handler(const boost::system::error_code& ec,boost::shared_ptr<boost::asio::ip::tcp::socket> sock)
    {
        if(ec)
        {
            std::cout << "id: " << _id << " connect wrong" << std::endl;
            return;
        }
        std::cout << "id: " << _id << " connect right to address:" << sock->remote_endpoint().address() << std::endl;
        sendtimer_=std::make_shared<boost::asio::deadline_timer>(m_io);
        sendtimer_->expires_from_now(boost::posix_time::milliseconds(500));
        sendtimer_->async_wait(boost::bind(&clients::do_write,this,sock));
    }
    void do_write(boost::shared_ptr<boost::asio::ip::tcp::socket> sock)
    {
        char sz_send[512]={0};
        sprintf(sz_send,"%d_hello",_id);
        m_buf.clear();
        for(int i=0; i<strlen(sz_send); i++)
        {
            m_buf.push_back(sz_send[i]);
        }
        sock->async_write_some(boost::asio::buffer(m_buf),boost::bind(&clients::write_handler,this,boost::asio::placeholders::error,sock));
    }
    void write_handler(const boost::system::error_code& ec,boost::shared_ptr<boost::asio::ip::tcp::socket> sock)
    {
        if(ec)
        {
            std::cout << "id: " << _id <<" connect disconnect" << std::endl;
        }
        else
        {
            sendtimer_->expires_from_now(boost::posix_time::milliseconds(500));
            sendtimer_->async_wait(boost::bind(&clients::do_write,this,sock));
        }
    }
private:
    boost::asio::io_service m_io;
    boost::asio::ip::tcp::endpoint m_ep;
    int _id;
    std::vector<char> m_buf;
    std::shared_ptr<boost::asio::deadline_timer> sendtimer_;
};

int main(int argc, char** argv)
{
    int id_val = 0;
    if(argc>1)
    {
        char* ch_val = argv[1];
        id_val = atoi(ch_val);
    }
    int nport = TARGET_VALUE_PORT;
    std::string str_address = TARGET_VALUE_IP;
    std::cout << "id: " << id_val << " client start" << std::endl;
    clients cl(str_address, nport, id_val);
    cl.run();
    std::cout << "id: " << id_val << " client end" << std::endl;
    return 0;

}
class servers
{
public:
    servers(boost::asio::io_service& io_ser, int nport):
        m_acceptor(io_ser, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), nport))
    {
        accept();
    }

    void accept()
    {
        std::cout << "prepare accept" << std::endl;

        boost::shared_ptr<boost::asio::ip::tcp::socket> sock( new boost::asio::ip::tcp::socket(m_acceptor.get_io_service()) );
        m_acceptor.async_accept(*sock, boost::bind(&servers::accept_handler, this, boost::asio::placeholders::error, sock));
    }

    void accept_handler(const boost::system::error_code& ec, boost::shared_ptr<boost::asio::ip::tcp::socket> sock)
    {
        if(ec)
        {
            return;
        }

        std::cout<< sock->remote_endpoint().address() << std::endl;
        sock->async_read_some(boost::asio::buffer(m_buf), boost::bind(&servers::read_handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, sock));

        accept();
    }

    void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred, boost::shared_ptr<boost::asio::ip::tcp::socket> sock)
    {
        if(ec)
        {
            std::cout << "error" << std::endl;
            return;
        }

        if(bytes_transferred==0)
        {
            std::cout << "empty" << std::endl;
        }
        else
        {
            std::string tmpstr(m_buf.begin(), m_buf.end());
            if(!tmpstr.empty())
            {
                std::cout << "Recv Data" << tmpstr << std::endl;
            }
            m_buf.clear();
        }

        sock->async_read_some(boost::asio::buffer(m_buf), boost::bind(&servers::read_handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, sock));
    }
private:
    boost::asio::ip::tcp::acceptor m_acceptor;
    std::vector<char> m_buf;
};

int main(int argc, char** argv)
{
    boost::asio::io_service io_service;
    int nport = LISTEN_VALUE_PORT;
    servers srv(io_service, nport);
    io_service.run();

    return 0;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • lianshaohua
    ztenv 2019-06-12 17:11
    已采纳

    没细看,可以对比一下boost自带的例子,有类似的代码的。

    点赞 评论
  • dabocaiqq
    dabocaiqq 2019-06-11 11:08
    点赞 评论

相关推荐