vendredi 24 juillet 2015

Run threads with boost and asio

Introduction

I'm trying create a thread to receive data from socket.

Code

void ClientTcp::read_handler(tcp::socket s){
    for(;;){
        char buffer[max_buffr];
        boost::system::error_code error;
        size_t length = s.read_some(boost::asio::buffer(buffer), error);
        if (error == boost::asio::error::eof)
            break; // Connection closed cleanly by peer.
        else if (error)
            throw boost::system::system_error(error); // Some other error.
        buffer[length] = '\0';
        std::cout << "-> " << buffer << std::endl;
    }
}

void ClientTcp::run(){
    tcp::socket s(io_service_);
    tcp::resolver resolver(io_service_);
    boost::asio::connect(s, resolver.resolve({ip_, port_}));

    boost::thread read_th(read_handler, std::move(s));
    std::string message;
    for(;;){
        std::cin >> message;
        boost::asio::write(s, boost::asio::buffer(message, message.size()));
    }
    read_th.join();
}

Problem

This line boost::thread read_th(read_handler, std::move(s)); has a bug that i can't understand.

Error no matching function for call to boost::thread::thread(<unresolved overloaded function type, std::remove_reference<boost::asio::basic_stream_socket<boost::asio::ip::tcp>&>::type)

Main Question How I launch a thread from class method ?

Aucun commentaire:

Enregistrer un commentaire