boost::asio::read function hanging

北慕城南 提交于 2021-02-05 09:12:09

问题


If someone could please help me out, I cannot understand how the boost::asio::read function works in boost asio. In boost's example they have it declare the buffer size before the message is received which makes no sense (how do I know how many bytes to read before I read the message?)

I tried this code but it just hangs

  boost::asio::io_service io_service;

  tcp::resolver resolver(io_service);
  tcp::resolver::query query(tcp::v4(), "localhost", "3000");
  tcp::resolver::iterator iterator = resolver.resolve(query);

  tcp::socket sock(io_service);
  boost::asio::connect(sock, iterator);
  cout << "read start" << endl;
  boost::system::error_code err_code;

  // Read from client.
  boost::asio::streambuf read_buffer;
  int bytes_transferred = boost::asio::read(sock, read_buffer, err_code);
  std::cout << "Read: " << make_string(read_buffer) << std::endl;
  read_buffer.consume(bytes_transferred); // Remove data that was read.

回答1:


Usually, you know how much bytes you want to read from the definition of protocol.

In the opposite case you have to read bytes one by one and observe the error code to stop at the end of connection, file, etc.

Synchronous methods (for example read) in Boost.Asio are nice for short examples, but in practical use cases you should prefer the asynchronous version async_read, which allows you to cancel or just wait for a next chunk of data without blocking your program.




回答2:


I've encountered the same problem. It seems that boost::asio::read is supposed to be hang there until the connected client is closed. Instead, you can use socket.read_some like this:

using boost::asio::ip::tcp;
try {
    boost::asio::io_service io_service;

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 9999));

    for (;;) {
        tcp::socket socket(io_service);
        acceptor.accept(socket);

        std::string message = "server received!\n";

        boost::system::error_code error_code;
        boost::asio::streambuf stream_buf;

        std::vector<char> buf(1024);
        size_t len = socket.read_some(boost::asio::buffer(buf), error_code);
        std::string received_filename(buf.begin(), buf.end());
        received_filename.resize(len);
        if (error_code) {
            std::cout << "error status: " << error_code.message() << std::endl;
        }

        boost::asio::write(socket, boost::asio::buffer(message), error_code);
        if (error_code) {
            std::cout << "error status: " << error_code.message() << std::endl;
        }
        query_database(tree, received_filename, output_folder, db_image_filenames);
    }
}
catch (std::exception& e) {
    std::cerr << e.what() << std::endl;
}

This will get the message from the client instantly with a relative short message. Hope this help.



来源:https://stackoverflow.com/questions/36678534/boostasioread-function-hanging

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!