gpt4 book ai didi

c++ - 从客户端到服务器的额外数据(json)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:25 28 4
gpt4 key购买 nike

我尝试发送字符串(作为 json 到服务器)。但是服务器得到这个带有一些额外数据的 json,如下所示:

(发送)

{    "#": "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;
}

(服务器)

   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()] = '\0';

关于c++ - 从客户端到服务器的额外数据(json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076326/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com