boost::asio::read() 永远阻塞

Posted

技术标签:

【中文标题】boost::asio::read() 永远阻塞【英文标题】:boost::asio::read() blocks forever 【发布时间】:2021-02-13 14:40:27 【问题描述】:

我正在尝试了解 Boost.asio 的工作原理。我已经编写了一个基本的服务器和客户端示例:

服务器.cpp

#include <iostream>
#include <string>
#include <boost/asio.hpp>

#define PORT 27015

using namespace boost::asio;
using ip::tcp;

std::string read(tcp::socket& socket) 
    boost::system::error_code error;
    boost::asio::streambuf buffer;
    boost::asio::read(socket, buffer, boost::asio::transfer_all(), error);
    if (error) 
        std::cerr << "read error: " << error.message() << "\n";
        return "ERROR";
    
    else 
        std::string data = boost::asio::buffer_cast<const char*>(buffer.data());
        return data;
    


void send(tcp::socket& socket, const std::string& message) 
    boost::system::error_code error;
    boost::asio::write(socket, boost::asio::buffer(message), error);
    if (error)
        std::cerr << "send error: " << error.message() << "\n";
    else
        std::cout << "sent \"" << message << "\" to the client" << "\n";


int main() 
    boost::asio::io_service io_service;

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), PORT)); // create listener for new connection(s)
    tcp::socket socket(io_service); // socket creation

    std::cout << "awaiting connection..." << "\n";
    acceptor.accept(socket); // direct connection(s) to the socket we created
    std::cout << "accepted connection!" << "\n";

    std::string received = read(socket); // receive data
    std::cout << "received message: " << received << "\n";

    send(socket, "hello from server!"); // send data

client.cpp

#include <iostream>
#include <string>
#include <boost/asio.hpp>

#define PORT 27015

using namespace boost::asio;
using ip::tcp;

int main(int argc, char *argv[])

    boost::asio::io_service io_service;
    tcp::socket socket(io_service); // socket creation

    std::string server_ipv4_address = "192.168.1.2";
    std::cout << "connecting to server at " << server_ipv4_address << "\n";

    try 
        socket.connect(tcp::endpoint(boost::asio::ip::address::from_string(server_ipv4_address), PORT)); // connection
        std::cout << "connected!" << "\n";
    
    catch (const boost::system::system_error& e) 
        std::cerr << "error while connecting: " << e.what() << "\n";
        return -1;
    

    boost::system::error_code error; // error holder

    std::string message = "hello from client!!\n";
    boost::asio::write(socket, boost::asio::buffer(message), error); // send message to server
    if (error)
        std::cerr << "send failed: " << error.message() << "\n";
    else
        std::cout << "sent \"" << message << "\" to the server" << "\n";

    boost::asio::streambuf receive_buffer;
    boost::asio::read(socket, receive_buffer, boost::asio::transfer_all(), error); // receive from server
    if (error && error != boost::asio::error::eof)
        std::cerr << "receive failed: " << error.message() << "\n";
    else 
        std::string data = boost::asio::buffer_cast<const char*>(receive_buffer.data());
        std::cout << "received data: " << data << "\n";
    

连接已正确建立,但来自服务器的 read() 函数阻止了程序,因为它没有从客户端接收数据,或者我调用它的方式有问题。 boost::asio::read() 这里的问题似乎是什么? 如果我将boost::asio::readboost::asio::read_until 交换,一切正常,如下所示。为什么该函数在客户端可以正常工作,而在服务端却不行?

std::string read(tcp::socket& socket) 
    boost::system::error_code error;
    boost::asio::streambuf buffer;
    boost::asio::read_until(socket, buffer, "\n");
    std::string data = boost::asio::buffer_cast<const char*>(buffer.data());
    return data;

【问题讨论】:

这就是IO读操作的本质,你阻塞直到收到数据。 【参考方案1】:

使用完成条件transfer_all读取意味着它将一直读取直到缓冲区已满或连接无效。

缓冲区“永远不会”满(因为它是 DynamicBuffer)。

这样就留下了客户端从不挂断的原因。

如果我将 boost::asio::read 与 boost::asio::read_until 交换,一切正常,如下所示。

没错。因为那时你还有另一个理由停止阅读。请注意,它仍然可能永远阻塞(当 '\n' 永远不会到达时)。

为什么该函数在客户端可以正常工作,而在服务器却不行?

它没有。这似乎是因为服务器显然确实关闭了连接(发出 EOF 信号)。 [您会注意到这一点,因为后续读取将返回 error_code boost::asio::error::eof。]

【讨论】:

谢谢,这正是我想要的。

以上是关于boost::asio::read() 永远阻塞的主要内容,如果未能解决你的问题,请参考以下文章

当 streambuf 由先前的 async_read 填充时, boost::asio::async_read 进入 boost::asio::streambuf 块

boost::asio::read 函数挂起

Boost::asio::async_read 不会在条件下停止

为啥 boost::asio::read 缓冲区数据大小小于读取大小?

boost::asio::async_read 不回调我的处理函数

可以 boost::asio::async_read_until 检测到无穷无尽的流