从客户端到服务器(JSON)的额外数据



我尝试将字符串发送(作为服务器)。但是服务器获得了此JSON的一些额外数据:

(发送)

{    "#":" 147",    " tableid":" sm_userkey"}

(got)

{    "#":" 147",    " tableid":" sm_userkey"}(/*/。如何解决此问题?

(客户端)

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

(服务器)

   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;

我从评论中的答案:

缓冲区未终止。尝试:

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

最新更新