Boost.Asio——(8)异步TCP服务端

2017-11-24  本文已影响311人  Savior2016
#include "stdafx.h"
#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}


//使用use shared_ptr 和 enable_shared_from_this来确保其他程序调用tcp_connection对象之前,它都是存在的
class tcp_connection
    : public boost::enable_shared_from_this<tcp_connection>
{
public:
    typedef boost::shared_ptr<tcp_connection> pointer;

    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new tcp_connection(io_service));
    }

    tcp::socket& socket()
    {
        return socket_;
    }

    //在这个函数里, boost::asio::async_write()用来服务于给客户端发送数据。使用 boost::asio::async_write()而不是 ip::tcp::socket::async_write_some(),来保证整块数据都被发送出去了
    void start()
    {
        //要发送的数据存储在message_中,因为我们需要在异步操作执行完之前,保持这个数据有效
        message_ = make_daytime_string();

        boost::asio::async_write(socket_, boost::asio::buffer(message_),
            boost::bind(&tcp_connection::handle_write, shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
        //去除不需要的参数,可以写成这样
        /*boost::asio::async_write(socket_, boost::asio::buffer(message_),
            boost::bind(&tcp_connection::handle_write, shared_from_this()));*/
    }

private:
    //此时所有与这个客户端相关的操作,都由handle_write()负责
    //error和bytes_transferred参数实际上没有使用,如果想的话,可以直接移除掉
    tcp_connection(boost::asio::io_service& io_service)
        : socket_(io_service)
    {
    }

    void handle_write(const boost::system::error_code& /*error*/,
        size_t /*bytes_transferred*/)
    {
    }
    //去除不需要的参数,可以写成下面这样
    /*void handle_write()
    {
    }*/

    tcp::socket socket_;
    std::string message_;
};


//tcp server 类
class tcp_server
{
public:
    //构造函数初始化了一个接收器监听本地的13端口
    tcp_server(boost::asio::io_service& io_service)
        : acceptor_(io_service, tcp::endpoint(tcp::v4(), 6300))
    {
        start_accept();
    }

private:
    //这个函数建立了一个socket并且初始化了一个异步的操作等待新连接
    void start_accept()
    {
        tcp_connection::pointer new_connection =
            tcp_connection::create(acceptor_.get_io_service());

        acceptor_.async_accept(new_connection->socket(),
            boost::bind(&tcp_server::handle_accept, this, new_connection,
                boost::asio::placeholders::error));
    }

    //这个函数在 start_accept()初始化异步接收操作之后被调用,它服务于客户端请求,并且调用 start_accept()初始化下一个接收操作
    void handle_accept(tcp_connection::pointer new_connection,
        const boost::system::error_code& error)
    {
        if (!error)
        {
            new_connection->start();
        }

        start_accept();
    }

    tcp::acceptor acceptor_;
};

int main()
{
    try
    {
        //创建了一个server用于接收连接进来的客户端
        boost::asio::io_service io_service;
        tcp_server server(io_service);
        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读