Boost :: asio :: async_write, writing data larger than 65536 bytes

I am trying to write jpeg frames over a socket to a client using async_write() . As a starting point, I used asynchronous TCP day server acceleration.

 #include <ctime> #include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; std::string make_daytime_string() { using namespace std; // For time_t, time and ctime; time_t now = time(0); return ctime(&now); } class tcp_connection : public boost::enable_shared_from_this<tcp_connection> { public: typedef boost::shared_ptr<tcp_connection> pointer; static pointer create(boost::asio::io_service& io_service) { return pointer(new tcp_connection(io_service)); } tcp::socket& socket() { return socket_; } void start() { message_ = make_daytime_string(); boost::asio::async_write(socket_, boost::asio::buffer(message_), boost::bind(&tcp_connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } private: tcp_connection(boost::asio::io_service& io_service) : socket_(io_service) { } void handle_write(const boost::system::error_code& /*error*/, size_t /*bytes_transferred*/) { } tcp::socket socket_; std::string message_; }; class tcp_server { public: tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13)) { start_accept(); } private: void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.io_service()); acceptor_.async_accept(new_connection->socket(), boost::bind(&tcp_server::handle_accept, this, new_connection, boost::asio::placeholders::error)); } void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error) { if (!error) { new_connection->start(); start_accept(); } } tcp::acceptor acceptor_; }; int main() { try { boost::asio::io_service io_service; tcp_server server(io_service); io_service.run(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; } 

I changed the method that executes async_write() as follows:

  void start() { // fileToVector method reads contents of file to vector; std::vector<unsigned char> message_ = fileToVector("/tmp/test"); boost::asio::async_write(socket_, boost::asio::buffer(message_), boost::bind(&tcp_connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } 

When reading a large file from the server using the client, the server will write a maximum of 65536 bytes. If I replaced the boost::asio::async_write() call with the synchronous boost::asio::write() call, the correct number of bytes will be sent to the client.

So, I suppose my question is: how can I send more than 65536 bytes using boost::asio::async_write() ? Any help would be greatly appreciated.

+8
boost boost-asio
source share
1 answer

The problem is that the use of data from the async_write function will not be sent immediately by the function, but some time after the start method finishes, the local variable message_ will be destroyed, and boost::asio::buffer will not copy the contents of message_ . It only stores a link to it. The result is unpredictable. 65,536 bytes can be transmitted, is the result of this behavior.

+9
source share

All Articles