client excerpt:
    socket_.async_connect(ip::tcp::endpoint(ip::address::from_string(ip), 
port), [this](boost::system::error_code ec) {

        if (!ec) {

            GOOGLE_PROTOBUF_VERIFY_VERSION;

            mcmessage::Query query;
            std::string key;
            std::string value;

            query.set_type(mcmessage::Query::SET);

            std::cout << "input the key: ";
            std::getline(std::cin, key);
            query.set_key(key.c_str(), key.size());

            std::cout << "input the value: ";
            std::getline(std::cin, value);
            query.set_value(value.c_str(), value.size());

            sendQuery(query);
        }
}

void Client::sendQuery(const mcmessage::Query &query)
{
     std::string buf;
     if (!query.SerializeToString(&buf)) {
        std::cerr << "Failed to send message." << std::endl;

        return;
     }
     std::cout << "buf length: " << buf.size() << std::endl;
     std::cout << "buf: " << buf << std::endl;
     int n = socket_.send(boost::asio::buffer(buf));
     std::cout << "n: " << n << std::endl;
}

server excerpt:

void Server::do_read() {
        char buf[1024];
        memset(buf, 0, sizeof(buf));
        boost::system::error_code ec2;
        int n = socket_.read_some(boost::asio::buffer(buf, sizeof(buf)), ec2
);
        
        if (!ec2) {
                std::cout << "n: " << n << std::endl;
                
                std::cout << "msg length: " << std::string(buf).size() << 
std::endl;
                std::cout << "msg: " << buf << std::endl;
        }
}

when i run it:

the client ouput is:

> input the key: hello
> input the value: world
> buf length: 16
> buf:hello▒world
> n: 16
>

and the server receives:

> n: 16
> msg length: 1
> msg:
>

the problem is, whaterver i do, the serialized buffer cannot reach the 
other side on the network, as you can see, the server receives 16 bytes but 
empty.

but if i change the send part of client to:
int n = socket_.send(boost::asio::buffer(std::string("test")));
then i can receive "test" at the server.

there are other strange property of this "buf" i serialize from protobuf:

if i use 
strcpy(str, buf.c_str());
the str would be empty.

if i use another std::string to copy from it, then this string acts 
normally at the client side, but also received as empty at the server side.

this is so confusing, please give me the enlightenment.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to