【发布时间】:2017-02-26 10:17:02
【问题描述】:
我正在尝试使用带有 iostream 的 boost 来制作流应用程序,但服务器没有分离图像帧 在接收循环中,将所有内容都放在一个文件中(不关闭文件,并继续接收同一文件中的其他帧)。 我能找到的唯一解决方案是发送一个用于连接的帧,但流式传输速度非常慢。
当前每个连接发送 1 个文件,一切正常(在远程网络上缓慢)
我想将其更改为每个连接发送多个文件(我认为我会在性能方面有所提升),但我遇到了上述问题。
“/tmp/img.frame”必须被覆盖
在我正在使用的代码下方(更改只是为了建立一个连接)
void send_()
{
boost::scoped_ptr<screenshot> ptr_screen(new screenshot);
handle_connection = true;
boost::asio::io_service svc;
boost::asio::ip::tcp::iostream stream_(boost::asio::ip::tcp::resolver::query{ "127.0.0.1", "6293" });
boost::iostreams::filtering_ostream out;
out.push(boost::iostreams::zlib_compressor());
out.push(stream_);
while (handle_connection) {
ptr_screen->Start(); // get screen.jpg
std::ifstream ifs("screen.jpg", std::ios::binary);
out << ifs.rdbuf();
out.flush();
ifs.close();
}
}
void receiver_()
{
connection_handle = true;
try
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 6293);
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);
if(!stream) { return; }
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::zlib_decompressor());
in.push(stream);
while(connection_handle)
{
std::ofstream ofs("/tmp/img.frame", std::ios::binary); // must be overwritten
copy(in, ofs);
ofs.close();
}
}
catch (std::exception& e)
{
std::cerr << "\n[-] " << e.what() << std::endl;
}
}
【问题讨论】: