我正在尝试将函数绑定到boost::asio::async_write
,但是我在 write.hpp
class Client{
public:
Client(const int &frame_,char* buf,const int& size_){
frame=frame_;
b=buf;
size=size_;
}
void doNothing(){
//?
}
void handle(const boost::system::error_code& error,std::size_t bytes_transferred ){
//Should handle the socket being closed properly and the data in the buffer being released
cout<<"Bytes sent: "<<bytes_transferred<<endl;
cout<<error<<endl;
}
void runClient()
{
try
{
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), host, port);
tcp::resolver::iterator iterator = resolver.resolve(query);
s=new boost::asio::ip::tcp::socket(io_service);
boost::asio::connect(*s, iterator);
std::cout << "Sending png: frame"<<frame<<" Size: "<<size<<"Bytes ... "<<endl;
int number_to_send = size; // Put your value
int converted_number = htonl(number_to_send);
boost::asio::async_write(*s,boost::asio::buffer(&converted_number, sizeof(converted_number)),&Client::doNothing);
boost::asio::async_write(*s, boost::asio::buffer(b, size),
boost::bind(&Client::handle,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
std::cout << "Done!"<<endl;
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
private:
enum { max_length = 1024 };
string port="1112";
string host="157.193.215.48";
char * b;
int size;
int frame;
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket* s;
};
此代码没有语法错误,但在编译时它确实在write.hpp中出现此错误:
/usr/local/include/boost/asio/impl/write.hpp:615:3:
Called object type 'void (Client::*)()' is not a function or function pointer.
受影响的代码:
template <typename AsyncWriteStream, typename ConstBufferSequence,
typename WriteHandler>
inline BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
void (boost::system::error_code, std::size_t))
async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
{
// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a WriteHandler.
BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
detail::async_result_init<
WriteHandler, void (boost::system::error_code, std::size_t)> init(
BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
detail::write_op<AsyncWriteStream, ConstBufferSequence,
detail::transfer_all_t, BOOST_ASIO_HANDLER_TYPE(
WriteHandler, void (boost::system::error_code, std::size_t))>(
s, buffers, transfer_all(), init.handler)(
boost::system::error_code(), 0, 1);
return init.result.get();
}
受影响的一行:
// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a WriteHandler.
BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
很明显,我的处理程序都存在问题,两者都有。我只是找不到什么,我真的很生气。
答案 0 :(得分:0)
在你的第一次异步写入中,你发送一个指向非静态成员函数的指针,而没有像第二次写入那样将它绑定到它的指针。
boost::asio::async_write(*s,boost::asio::buffer(&converted_number, sizeof(converted_number)),&Client::doNothing);
应该是
boost::asio::async_write(*s,boost::asio::buffer(&converted_number, sizeof(converted_number)),boost::bind(&Client::doNothing,this));