简体   繁体   中英

Extra data from client to server (json)

I try to send string (as json to server). But server get this json with some extra data like this:

(send)

{
    "#": "147",
    "TableID": "SM_userkey"
}

(got)

{
    "#": "147",
    "TableID": "SM_userkey"
}(/*/. How to resolve this problem?

(client)

std::stringstream ss;
boost::property_tree::write_json(ss, pt);


try{

boost::asio::ip::tcp::iostream *stream = new boost::asio::ip::tcp::iostream(ip_address,                                         boost::lexical_cast<std::string>(9858));
stream->write(ss.str().c_str(), ss.str().length());
stream->flush();
//    ss.flush();

}
catch (std::exception& e){
    std::cout << "Exception: " << e.what() << std::endl;
}

(server)

   boost::asio::io_service io_service;

        boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 9858);
        boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);

        boost::asio::ip::tcp::iostream stream;
        boost::system::error_code ec;
        acceptor.accept(*(stream.rdbuf()), ec); 

        while (!stream.rdbuf()->available()) {}

     //   std::cout << "bytes available: " << stream.rdbuf()->available() << std::endl;

        char buf[stream.rdbuf()->available()];
        stream.read(buf, stream.rdbuf()->available());
        stream.flush();          
        std::cout << buf << std::endl;

My answer from the comments:

Buffer is not null terminated. Try:

char buf[stream.rdbuf()->available()+1]; 
buf[stream.rdbuf()->available()] = '\0';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM