我尝试发送字符串(作为json发送到服务器)。但服务器得到这个json的一些额外的数据,如:
(发送)
{ "#": "147", "TableID": "SM_userkey" }
(GOT)
{ "#": "147", "TableID": "SM_userkey" }(/*/. How to resolve this problem?
(客户端)
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;
答案 0 :(得分:0)
我在评论中的回答:
缓冲区未终止。尝试:
char buf[stream.rdbuf()->available()+1];
buf[stream.rdbuf()->available()] = '\0';