我在使用 async_read_some 时遇到了困难
我用 async_write_some 编译并成功了。
但是,我尝试使用 async_read_some 进行编译,
并提出了奇怪的错误,它可以找到正确的方法。
我是提升世界的新手。请帮我解决这些问题。
提前致谢。
opps编译错误是这样的
错误:没有匹配函数调用 'ws_async_tcp_connection::async_recv_msg_some(boost::asio::mutable_buffers_1, boost::_bi::bind_t, boost::_bi::list3, boost::arg<1> ( )(), boost::arg<2> ( )()> >)' /home/chris/proto/inc/ws_async_tcp_connection.h:119: 注意:候选者是:void ws_async_tcp_connection::async_recv_msg_some(T&, Handler) [with T = boost ::asio::mutable_buffers_1, 处理程序 = boost::_bi::bind_t, boost::_bi::list3, boost::arg<1> ( )(), boost::arg<2> ( )()> > ]
header
-----------------------------------------------------------------------------------
class ws_async_tcp_connection{
boost::asio::ip::tcp::socket socket_;
public:
ws_async_tcp_connection(boost::asio::io_service& arg_io_service)
: socket_(arg_io_service){}
/*
* destructor
*/
~ws_async_tcp_connection(){
shutdown();
}
/*
* connect method
* : binding connection handler -> handle_connect
*/
void connect(const std::string& host_addr, const std::string& port_no){
boost::asio::ip::tcp::resolver resolver_(socket_.get_io_service());
boost::asio::ip::tcp::resolver::query query_(boost::asio::ip::tcp::v4(), host_addr, port_no);
boost::asio::ip::tcp::resolver::iterator end_point_iter = resolver_.resolve(query_);
boost::asio::async_connect(socket_, end_point_iter,
boost::bind(&ws_async_tcp_connection::handle_connect, this, boost::asio::placeholders::error));
}
/*
* handle_connect
* : handle after the connect method called
*/
void handle_connect(const boost::system::error_code& ec){
if(!ec){
// successfully established the connection - maybe print the remote ip, port
/* getting the remote_endpoint ip, port
std::string connected_server_ip = socket_.remote_endpoint().address().to_string();
unsigned short connected_server_port = socket_.remote_endpoint().port();
*/
}
else{
// error handling - maybe use Logger class to print the error
shutdown();
}
}
/*
* set_option
* : set option for the tcp socket
*/
template<typename SettableSocketOption>
void set_option(const SettableSocketOption& option){
// if performance is poor, then let's try no_delay option(true);
/* keepalive option
boost::asio::socket_base::keep_alive option(true);
socket.set_option(option);
*/
socket_.set_option(option);
}
/*
* is_open
* : socket is opened or not
*/
bool is_open(){
return socket_.is_open();
}
/*
* async_send_msg
* : send msg (warning! : will be blocked until the exact bytes you ask for are transmitted)
*/
template <typename T, typename Handler>
void async_send_msg(const T& send_buf, Handler handler){
boost::asio::async_write(socket_, send_buf, handler);
}
/*
* async_recv_msg
* : recv msg (warning! : will be blocked until the exact bytes you ask for are transmitted)
*/
template <typename T, typename Handler>
void async_recv_msg(T& recv_buf, Handler handler){
boost::asio::async_read(socket_, recv_buf, handler);
}
/*
* async_send_msg_some
* : send msg
*/
template <typename T, typename Handler>
void async_send_msg_some(const T& send_buf, Handler handler){
socket_.async_write_some(send_buf, handler);
}
/*
* async_recv_msg_some
* : recv msg (Typically use this if you don't have to receive the exact numbers of bytes)
*/
template <typename T, typename Handler>
void async_recv_msg_some(T& recv_buf, Handler handler){
socket_.async_read_some(recv_buf, handler);
}
/*
* shutdown
*/
void shutdown(){
if(is_open()){
boost::system::error_code ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
if(ec){
// may print shutdown error
}
socket_.close();
}
}
/*
* socket
* : returning the current holding socket
*/
boost::asio::ip::tcp::socket& socket(){
return socket_;
}
};
typedef boost::shared_ptr<ws_async_tcp_connection> async_tcp_ptr;
-------------------------------------------------------------------------------
src
-------------------------------------------------------------------------------
class async_tcp_test_server{
enum{ buf_size = 1024 };
boost::asio::ip::tcp::acceptor acceptor_;
boost::array<char, buf_size> send_buf;
boost::array<char, buf_size> recv_buf;
public:
async_tcp_test_server(boost::asio::io_service& io_service, unsigned short port_no)
: acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port_no)){
// start an accept operation for a new connection
async_tcp_ptr new_conn(new ws_async_tcp_connection(acceptor_.get_io_service()));
acceptor_.async_accept(new_conn->socket(), boost::bind(&async_tcp_test_server::handle_accept, this,
boost::asio::placeholders::error, new_conn));
}
void handle_accept(const boost::system::error_code& ec, async_tcp_ptr conn){
if(!ec){
send_buf.assign(0x00);
string hello_str = "Welcome to connect Asynchronous TCP Test Server :)";
memcpy(&send_buf[0], hello_str.c_str(), hello_str.length());
conn->async_send_msg(boost::asio::buffer(send_buf), boost::bind(&async_tcp_test_server::handle_write, this, boost::asio::placeholders::error, conn));
//conn->async_send_msg_some(boost::asio::buffer(send_buf), boost::bind(&async_tcp_test_server::handle_write, this, boost::asio::placeholders::error,
//boost::asio::placeholders::bytes_transferred, conn));
}
// start an accept operation for new connection
async_tcp_ptr new_conn(new ws_async_tcp_connection(acceptor_.get_io_service()));
acceptor_.async_accept(new_conn->socket(), boost::bind(&async_tcp_test_server::handle_accept, this,
boost::asio::placeholders::error, new_conn));
}
void handle_write(const boost::system::error_code& ec, async_tcp_ptr conn){
if(!ec){
recv_buf.assign(0x00);
conn->async_recv_msg_some(boost::asio::buffer(recv_buf), boost::bind(&async_tcp_test_server::handle_read, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
void handle_read(const boost::system::error_code& ec, size_t num_recv_bytes){
cout << recv_buf.data() << endl;
}
};
int main(void)
{
unsigned short port_no = 7807;
boost::asio::io_service io_svr;
async_tcp_test_server test_server(io_svr, port_no);
io_svr.run();
}
这是错误的输出(在我更改了 T& -> const T& 之后)
/home/chris/proto/boost/boost/bind/bind_template.hpp:15: 从 'boost::_bi::bind_t, boost::arg<1> ( )(), boost::arg<2> ( )(), boost::_bi::value > > >' async_tcp_test_server.cpp:46: 从这里实例化 /home/chris/proto/boost/boost/bind/bind.hpp:69: 错误: 'void (async_tcp_test_server: :*)(const boost::system::error_code&, size_t)' 不是类、结构或联合类型 /home/chris/proto/boost/boost/asio/basic_stream_socket.hpp:在成员函数 'void boost: :asio::basic_stream_socket::async_read_some(const MutableBufferSequence&, const ReadHandler&) [with MutableBufferSequence = boost::asio::mutable_buffers_1, ReadHandler = boost::_bi::bind_t, boost::arg<1> ( )(), boost ::arg<2> ()(), boost::_bi::value > > >, 协议 = boost::asio::ip::tcp, StreamSocketService = boost::asio::stream_socket_service]': /home/chris/proto/inc/ws_async_tcp_connection .h:120:从 'void ws_async_tcp_connection::async_recv_msg_some(const T&, Handler) 实例化 [with T = boost::asio::mutable_buffers_1, Handler = boost::_bi::bind_t, boost::arg<1> ( ) (), boost::arg<2> ( )(), boost::_bi::value > > >]' async_tcp_test_server.cpp:46: 从这里实例化 /home/chris/proto/boost/boost/asio/basic_stream_socket .hpp:785:错误:不匹配调用 '(boost::_bi::bind_t, boost::arg<1> ( )(), boost::arg<2> ()(), boost::_bi::value > > >) (const boost::system::error_code&, const long unsigned int&)' /home/chris/proto/boost/boost/asio/detail/bind_handler.hpp:在成员函数 'void boost::asio::detail::binder2::operator()() [with Handler = boost::_bi::bind_t, boost::arg<1> ( )(), boost::arg <2> ( )(), boost::_bi::value > > >, Arg1 = boost::system::error_code, Arg2 = long unsigned int]': /home/chris/proto/boost/boost/asio/ handler_invoke_hook.hpp:64: 实例化自 'void boost::asio::asio_handler_invoke(Function, ...) [with Function = boost::asio::detail::binder2, boost::arg<1> ( )() , 升压::arg<2> ()(), boost::_bi::value > > >, boost::system::error_code, long unsigned int>]' /home/chris/proto/boost/boost/asio/detail/handler_invoke_helpers.hpp:39:从 'void boost_asio_handler_invoke_helpers::invoke(Function&, Context&) 实例化 [with Function = boost::asio::detail::binder2, boost::arg<1> ( )(), boost::arg<2> ( )( ), boost::_bi::value > > >, boost::system::error_code, long unsigned int>, 上下文 = boost::_bi::bind_t, boost::arg<1> ( )(), boost: :arg<2> ()(), boost::_bi::value > > >]' /home/chris/proto/boost/boost/asio/detail/reactive_socket_recv_op.hpp:110: 实例化自 'static void boost::asio::detail: :reactive_socket_recv_op::do_complete(boost::asio::detail::io_service_impl*, boost::asio::detail::operation*, const boost::system::error_code&, size_t) [with MutableBufferSequence = boost::asio: :mutable_buffers_1, 处理程序 = boost::_bi::bind_t, boost::arg<1> ( )(), boost::arg<2> ()(), boost::_bi::value > > >]' /home/chris/proto/boost/boost/asio/detail/reactive_socket_recv_op.hpp:80: 从 'boost::asio::detail::reactive_socket_recv_op 实例化::reactive_socket_recv_op(boost::asio::detail::socket_type, boost::asio::detail::socket_ops::state_type, const MutableBufferSequence&, int, Handler&) [with MutableBufferSequence = boost::asio::mutable_buffers_1, Handler = boost::_bi::bind_t, boost::arg<1> ( )(), boost::arg<2> ()(), boost::_bi::value > > >]' /home/chris/proto/boost/boost/asio/detail/reactive_socket_service_base.hpp:273: 从 'void boost::asio::detail:: 实例化reactive_socket_service_base::async_receive(boost::asio::detail::reactive_socket_service_base::base_implementation_type&, const MutableBufferSequence&, int, Handler) [with MutableBufferSequence = boost::asio::mutable_buffers_1, Handler = boost::_bi::bind_t, boost: :arg<1> ( )(), boost::arg<2> ()(), boost::_bi::value > > >]' /home/chris/proto/boost/boost/asio/stream_socket_service.hpp:318: 从'void boost::asio::stream_socket_service::async_receive(类型名 boost::asio::detail::reactive_socket_service::implementation_type&, const MutableBufferSequence&, int, const ReadHandler&) [with MutableBufferSequence = boost::asio::mutable_buffers_1, ReadHandler = boost::_bi::bind_t, boost::arg< 1> ( )(), boost::arg<2> ( )(), boost::_bi::value > > >, 协议 = boost::asio::ip::tcp]' /home/chris/proto /boost/boost/asio/basic_stream_socket.hpp:787: 实例化自 'void boost::asio::basic_stream_socket::async_read_some(const MutableBufferSequence&, const ReadHandler&) [with MutableBufferSequence = boost::asio::mutable_buffers_1,ReadHandler = boost::_bi::bind_t, boost::arg<1> ()(), boost::arg<2> ( )(), boost::_bi::value > > >, 协议 = boost::asio::ip::tcp, StreamSocketService = boost::asio::stream_socket_service] ' /home/chris/proto/inc/ws_async_tcp_connection.h:120: 实例化自 'void ws_async_tcp_connection::async_recv_msg_some(const T&, Handler) [with T = boost::asio::mutable_buffers_1, Handler = boost::_bi:: bind_t, boost::arg<1> ( )(), boost::arg<2> ( )(), boost::_bi::value > > >]' async_tcp_test_server.cpp:46: 从这里 /home/ 实例化chris/proto/boost/boost/asio/detail/bind_handler.hpp:118:错误:不匹配调用 '(boost::_bi::bind_t, boost::arg<1> ( )(), boost:: arg<2> ( )(), boost::_bi::value