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