c++ - Problems with Async_Read function -


introduction

i'm studying c++ , boost/asio implement distributed system, need create asynchronous tcp server. server works echo-server need make request first (for example, send text through socket) , server responds pi number.

tcp-connection class used in main-server class

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_;             }              void start(){                 for(;;){                      boost::asio::async_read(socket_, boost::asio::buffer(buffer_),                         boost::bind(&tcp_connection::handle_read, shared_from_this(),                          boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)                     );                      buffer_ = "pi: 3.1415\n\0";                     boost::asio::async_write(socket_, boost::asio::buffer(buffer_),                         boost::bind(&tcp_connection::handle_write, shared_from_this(),                             boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));                 }             }         private:             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_read(){             }              //attr             tcp::socket socket_;             std::string buffer_;     }; 

problem

i've read tutorials , many questions in stackoverflow. , can't understand why i've bug: error: ‘void (tcp_connection::*)()’ not class, struct, or union type.

i found error source in line: boost::asio::async_read(...). if remove line, server works fine, need understand async_read functionality implement complex systems later.

your async_read expects handler - function called when read socket. handler should have specific signature. in case should

void handle_read(boost::system::error_code const& ec, size_t bytes_transferred) 

just because use

boost::bind(&tcp_connection::handle_read, shared_from_this(),                      boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred) 

so bind tcp_connection::handle_read this (kind of) (the first implicit argument non-static method) , error code , number of bytes transferred, expected signature void handle_read(boost::system::error_code const& ec, size_t bytes_transferred). maybe need learn more how binding works: http://www.boost.org/doc/libs/1_58_0/libs/bind/doc/html/bind.html

as bonus:

your

for (;;)  {     //read     // write } 

is synchronous approach. need different if want use async one. main idea chain them read takes handler , handler async writes response taking write handler , write handler starts async read, , looped.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -