[英]Extra data from client to server (json)
I try to send string (as json to server). 我尝试将字符串(作为json发送到服务器)。 But server get this json with some extra data like this:
但是服务器会用一些额外的数据来获取此json,例如:
(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. 缓冲区不是以null终止的。 Try:
尝试:
char buf[stream.rdbuf()->available()+1];
buf[stream.rdbuf()->available()] = '\0';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.