2
votes

I have a problem with sending data from my client based on boost asio.
1. I am using protobuffer to serialize data.
2. I am using write function:
boost::asio::write(socket, data, boost::asio::transfer_exactly(65536));
As I understand I cant use this method in my case. I have following mistakes after compilation:

c:\local\boost_1_55_0\boost\asio\buffer.hpp(266): could be 'boost::asio::const_buffer &boost::asio::const_buffer::operator =(const boost::asio::const_buffer &)' 1> while trying to match the argument list '(boost::asio::const_buffer, char)'

I am using this client for sending data:

#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include<windows.h>
#include<conio.h>
#include <thread>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include "test.pb.h"
#include <boost/thread/thread.hpp>
//#include "test.pb.cc" - this was the problem (you should only include *.h files)
using namespace std;
using boost::asio::ip::tcp;



int main()
{
try {

// connect to the server:
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
string server_address = "localhost";
string server_port = "55555";

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("some name");

cout << p.id();
cout << p.name();
string data; // this will hold serialized data
    bool ok = p.SerializeToString(&data);
string buffer;
int size = 1024;
void handler(
    const boost::system::error_code& error, // Result of operation.
    std::size_t bytes_transferred           // Number of bytes read.
    ); 
// send a chunk of a particular size
//int bytes_to_send = std::min(3056, data.size());
cout << data.size() << endl;  // shows amount of remaining data
boost::asio::write(socket, data, boost::asio::transfer_exactly(65536));
cout << data.size() << endl;  // shows a reduction in amount of remaining data

}

     }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }
    cout << "\nClosing";
    _getch();
}
1
Please make your code self-contained. Protobuf has nothing to do with it. - sehe

1 Answers

0
votes

You need to adapt the string for use as an Asio buffer:

boost::asio::write(socket, boost::asio::buffer(data), boost::asio::transfer_exactly(65536));

write returns the number of chars written.

It looks a bit like you actually want something like boost::asio::streambuf

See it Live On Coliru

#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.cc" - this was the problem (you should only include *.h files)
//#include "test.pb.h"

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

struct Person {
    int id() {return 42;}
    const char* name() {return "name";}
    void set_id(int) {}
    void set_name(const char*) {}

    bool SerializeToString(std::string* data) const { if (data) *data += "someserializedform"; return data; }
};

int main()
{
    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    = "55555";

        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("some name");

            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::transfer_exactly(65536));
            std::cout << data.size() << std::endl;  // shows a reduction in amount of remaining data
        }

    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    std::cout << "\nClosing";
    std::string dummy;
    std::cin >> dummy;
}