Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost beast中的多部分边界_C++_Http_Boost_Webserver_Boost Beast - Fatal编程技术网

C++ boost beast中的多部分边界

C++ boost beast中的多部分边界,c++,http,boost,webserver,boost-beast,C++,Http,Boost,Webserver,Boost Beast,我正在尝试在本地托管服务器上以MJPEG格式流式传输视频中的帧。以下是我的代码: #include <iostream> #include <string> #include <memory> #include <thread> #include <cstdlib> #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #inc

我正在尝试在本地托管服务器上以MJPEG格式流式传输视频中的帧。以下是我的代码:

#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <cstdlib>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/config.hpp>

using boost::asio::ip::tcp;
namespace http = boost::beast::http;

cv::Mat frame;
std::vector<uchar> buffer;

int main(){
    try{
        boost::asio::io_context io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));
        boost::system::error_code err;
        tcp::socket socket(io_service);
        acceptor.accept(socket);

        boost::beast::flat_buffer request_buffer;

        cv::VideoCapture cap("zz.mp4");

        // http::request<http::string_body> req;
        // http::read(socket, request_buffer, req, err);
        // if(err){
        //     std::cerr << "read: " << err.message() << "\n";
        // } 

        while(cap.isOpened()){
            cap >> frame;
            cv::imencode(".jpg", frame, buffer, std::vector<int> {cv::IMWRITE_JPEG_QUALITY, 95});

            auto const size = buffer.size();

            http::request<http::string_body> req;
            http::read(socket, request_buffer, req, err);
            if(err){
                std::cerr << "read: " << err.message() << "\n";
                break;
            } 

            http::response<http::empty_body> res{http::status::ok, req.version()};
            res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
            // res.set(http::field::content_type, "multipart/x-mixed-replace; boundary=frame\r\n\r\n--frame\n");
            res.set(http::field::content_type, "multipart/x-mixed-replace; boundary=frame");
            res.content_length(size);
            http::response_serializer<http::empty_body> sr{res};
            http::write_header(socket, sr);
            while(true){
                http::response<http::vector_body<unsigned char>> res{std::piecewise_construct,
                            std::make_tuple(std::move(buffer)),
                            std::make_tuple(http::status::ok, req.version())};
                res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
                res.set(http::field::content_type, "image/jpeg");
                res.content_length(size);
                res.keep_alive(req.keep_alive());
                http::write(socket, res, err);
            }
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::tcp;
命名空间http=boost::beast::http;
cv::垫架;
向量缓冲区;
int main(){
试一试{
boost::asio::io_上下文io_服务;
tcp::acceptor acceptor(io_服务,tcp::endpoint(tcp::v4(),1112));
boost::system::error\u code err;
tcp::套接字(io_服务);
接受(套接字);
boost::beast::flat\u buffer request\u buffer;
cv::视频捕获帽(“zz.mp4”);
//http::请求请求;
//http::read(套接字、请求缓冲区、请求、错误);
//如果(错误){

//std::cerr答案如下。我们需要使用
boost::beast::http::field::body设置边界

#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <cstdlib>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/config.hpp>

using boost::asio::ip::tcp;
namespace http = boost::beast::http;

cv::Mat frame;
std::vector<uchar> buffer;
cv::Mat grayframe;

int main(){
    try{
        boost::asio::io_context io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));
        boost::system::error_code err;
        tcp::socket socket(io_service);
        acceptor.accept(socket);

        boost::beast::flat_buffer request_buffer;

        cv::VideoCapture cap("zz.mp4");

        http::request<http::string_body> req;
        http::read(socket, request_buffer, req, err);
        if(err){
            std::cerr << "read: " << err.message() << "\n";
        } 

        http::response<http::empty_body> res{http::status::ok, req.version()};
        res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
        res.set(http::field::content_type, "multipart/x-mixed-replace; boundary=frame");
        res.keep_alive();
        http::response_serializer<http::empty_body> sr{res};
        http::write_header(socket, sr);

        while(cap.isOpened()){
            cap >> frame;
            cv::cvtColor(frame, grayframe, CV_BGR2GRAY);
            cv::imencode(".jpg", grayframe, buffer, std::vector<int> {cv::IMWRITE_JPEG_QUALITY, 95});

            auto const size = buffer.size();

            http::response<http::vector_body<unsigned char>> res{std::piecewise_construct,
                        std::make_tuple(std::move(buffer)),
                        std::make_tuple(http::status::ok, req.version())};
            res.set(http::field::body, "--frame");
            res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
            res.set(http::field::content_type, "image/jpeg");
            res.content_length(size);
            res.keep_alive(req.keep_alive());
            http::write(socket, res, err);
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::tcp;
命名空间http=boost::beast::http;
cv::垫架;
向量缓冲区;
cv::Mat灰色框架;
int main(){
试一试{
boost::asio::io_上下文io_服务;
tcp::acceptor acceptor(io_服务,tcp::endpoint(tcp::v4(),1112));
boost::system::error\u code err;
tcp::套接字(io_服务);
接受(套接字);
boost::beast::flat\u buffer request\u buffer;
cv::视频捕获帽(“zz.mp4”);
http::请求请求;
http::read(套接字、请求缓冲区、请求、错误);
如果(错误){
标准:cerr