使用protobuf与boost asio

时间:2014-06-19 01:34:52

标签: c++ boost client boost-asio

我在根据boost asio从我的客户端发送数据时遇到问题 1.我正在使用protobuffer来序列化数据 2.我正在使用写功能:
boost::asio::write(socket, data, boost::asio::transfer_exactly(65536));
据我所知,我不能在我的情况下使用这种方法。编译后我有以下错误:

  

c:\ local \ boost_1_55_0 \ boost \ asio \ buffer.hpp(266):可能是' boost :: asio :: const_buffer& boost :: asio :: const_buffer :: operator =(const boost :: asio :: const_buffer&)'   1 GT;试图匹配参数列表'(boost :: asio :: const_buffer,char)'

我正在使用此客户端发送数据:

#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 个答案:

答案 0 :(得分:0)

您需要调整字符串以用作Asio缓冲区:

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

write返回写入的字符数。

看起来有点像你真正想要boost::asio::streambuf

之类的东西

查看 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;
}