Question

I'm trying to bind a function to boost::asio::async_write, but I got a semantic error in 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;

};

This code has no syntax errors, but it does give this error in write.hpp when compiling:

/usr/local/include/boost/asio/impl/write.hpp:615:3:
   Called object type 'void (Client::*)()' is not a function or function pointer.

Affected code:

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();
}

Affected line:

// 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;

It's obvious that there's something wrong with my handlers, both of them. I just can't find out what and I'm getting really annoyed by it.

Was it helpful?

Solution

in your first async write, you send a pointer to a non static member function without having binded it to its this pointer like you did in the second write.

    boost::asio::async_write(*s,boost::asio::buffer(&converted_number, sizeof(converted_number)),&Client::doNothing);

should be

    boost::asio::async_write(*s,boost::asio::buffer(&converted_number, sizeof(converted_number)),boost::bind(&Client::doNothing,this));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top