user3735525
user3735525

Reputation: 31

Reading data with boost asio on client

I am learning boost asio and have mistake. I have written simple client ( I can send data from it but when I read data I cant even compile it) I used protocol buffer to serialize data . So file #include "test.pb.h" is probuffer class

My code client :

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include "test.pb.h"
using boost::asio::ip::tcp;

int main(int argc, char** argv)
{
try
{
    // connect to the server:
    boost::asio::io_service io_service;
    tcp::resolver resolver(io_service);
    std::string const server_address = "localhost";
    std::string const server_port    = "10000";

    tcp::resolver::query query(server_address, server_port);
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::socket socket(io_service);
    boost::asio::connect(socket, endpoint_iterator);
    //while ( true)
    {
        Person p = Person();
        p.set_id(22);
        p.set_name("Jack vorobey");

     //   std::cout << p.id();
   //     std::cout << p.name();

        std::string data; // this will hold serialized data
        bool ok = p.SerializeToString(&data);
        assert(ok);

        //    std::cout << data.size() << std::endl;
        boost::asio::write(socket, boost::asio::buffer(data)) 

        boost::asio::read(socket, boost::asio::buffer(data));;




   // break; // Connection closed cleanly by peer.

  //      std::cout << data.size() << std::endl;  // shows a reduction in amount of   
remaining data
//      boost::asio::read(socket, boost::asio::buffer(data) /*,  
    }
boost::asio::transfer_exactly(65536) */);
}
catch (std::exception& e)
{
    //std::cerr << e.what(luuu) << std::endl;
}
std::cout << "\nClosing";
std::string dummy;
}

The code of my mistake I dont understand : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const boost::asio::const_buffer' (or there is no acceptable conversion) 1> c:\local\boost_1_55_0\boost\asio\buffer.hpp(136): could be 'boost::asio::mutable_buffer &boost::asio::mutable_buffer::operator =(const boost::asio::mutable_buffer &)' 1> while trying to match the argument list '(boost::asio::mutable_buffer, const boost::asio::const_buffer)'

Upvotes: 0

Views: 1936

Answers (2)

sehe
sehe

Reputation: 393134

You can use a streambuf, or specify the (preallocated!) size:

#include <boost/asio.hpp>
#include <string>

using boost::asio::ip::tcp;

int main()
{
    boost::asio::io_service io_service;
    tcp::resolver resolver(io_service);

    tcp::socket socket(io_service);
    boost::asio::connect(socket, resolver.resolve(tcp::resolver::query("localhost", "10000")));

    std::string request("request");
    boost::asio::write(socket, boost::asio::buffer(request));

#if 0
    std::string response;
    response.resize(32);
    boost::asio::read(socket, boost::asio::buffer(&response[0], response.size()));
#else
    boost::asio::streambuf response;
    boost::asio::read(socket, response);
#endif
}

Upvotes: 1

Igor R.
Igor R.

Reputation: 15075

This is because template<typename Elem, typename Traits, typename Allocator> const_buffers_1 buffer(const std::basic_string<Elem, Traits, Allocator> &) returns an instance of const_buffers_1 (which is a model of ConstBufferSequence concept). Certainly, you cannot read data into a constant buffer.

Do not read data into a std::string, because it's not intended for that (note that its c_str() and data() member functions return const char*). Instead, allocate another buffer or use asio::streambuf.

Upvotes: 1

Related Questions