1

I'm trying to make a streaming application using boost with iostream, but the server is not separating the image frame in the receive loop, is getting everything in one file(does not close the file, and continues to receive the other frames in the same file). The only solution i could find was sent a frame for connection, but is leaving the streaming very slow.

currently sending 1 file per connection and everything works (slowly on remote networks)

i want to change it to send multiple files per connection (I think i will have a gain in performance), but I'm having the problem mentioned above.

the "/tmp/img.frame" must be overwritten

below the code I'm using (changed just to make one connection)

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;
    }
}
Melissia_M
  • 323
  • 2
  • 13

1 Answers1

2

Regardless of the underlying technology, you have to realize that TCP. Is a streaming, message-less protocol. If you want to send individual messages of any sort and have them picked apart correctly at the receding end, you have to implement an application protocol of some sort:

  • length word prefix
  • type-length-value
  • STX/ETX, with escaping in full detail
  • self-describing protocol such as XML

etc. etc. etc.

user207421
  • 305,947
  • 44
  • 307
  • 483