【问题标题】:Reusing ASIO connection, read_some exception重用 ASIO 连接,read_some 异常
【发布时间】:2013-04-18 20:56:29
【问题描述】:

我目前正在尝试弄清楚如何正确重用 asio 套接字。我能够成功发出请求,并得到结果。第二次发出请求时,我得到一个异常:read_some: End of file。第二次写入似乎工作正常,我看到第二个 http 请求通过wireshark 发出。我认为套接字上的剩余信息会以某种方式破坏我的连接。对此问题的任何帮助将不胜感激。这是我正在使用的代码:

persistent_connection::persistent_connection(std::string ip, std::string port):
io_service_(), socket_(io_service_), is_setup_(false)
{
    boost::asio::ip::tcp::resolver resolver(io_service_);
    boost::asio::ip::tcp::resolver::query query(ip,port);
    boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
    boost::asio::ip::tcp::endpoint endpoint = *iterator;
    socket_.async_connect(endpoint, boost::bind(&persistent_connection::handler_connect, this, boost::asio::placeholders::error, iterator));
    io_service_.run();
}


void persistent_connection::handler_connect(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
    if(ec)
    {
        std::cout << "Couldn't connect" << ec << std::endl;
        return;
    }
    else
    {
        boost::asio::socket_base::keep_alive keep_option(true);
        socket_.set_option(keep_option);
    }
}


void persistent_connection::write(std::string message)
{
    std::string request_stream = "GET /" + message + " HTTP/1.0\r\n";
    request_stream += "HOST: 10.1.10.220";
    request_stream += "Accept: */*\r\n";
    request_stream += "Connection: keep-alive\r\n\r\n";

    try
    {
        boost::asio::write(socket_, boost::asio::buffer(request_stream, request_stream.size()));
    }catch(std::exception& e)
    {
        std::cout << "Write exception: " << e.what() << std::endl;
    }

    boost::array<char,8192> buf;
    try
    {
        socket_.read_some(boost::asio::buffer(buf));
    }catch(std::exception& e)
    {
        std::cout << "Read exception: " << e.what() << std::endl;
    }

    std::string response = buf.data();
    std::cout << response << std::endl;
}

编辑:添加主函数。

int main()
{
    persistent_connection p("10.1.10.220", "80");
    std::string check;
    do
    {
        std::cin >> check;
        if(check.compare("s") == 0)
        {
            std::cout << "Sending" << std::endl;
            p.write("100");
        }
    }while(check.compare("x") != 0);
}

【问题讨论】:

  • 不太清楚你如何使用上面的代码。 “第一次”在哪里结束,“第二次”从哪里开始?你确定对端在第一次之后没有关闭连接吗?
  • 抱歉,省略了那部分。基本上第一次调用 write ,一切运行良好。下次我尝试使用 write 时,出现读取异常。
  • 这意味着http服务器在第一个请求完成后关闭连接,即它不尊重“Connection: keep-alive”标头。当您收到第一个响应时,您是否看到“Connection: keep-alive”标头?您是否尝试请求“HTTP/1.1”?
  • 谢谢,看来我测试的服务器不支持keep alive,这就是问题所在。
  • 但尝试请求 HTTP/1.1 - 此版本默认支持 keep-alive。

标签: http boost network-programming boost-asio http-1.0


【解决方案1】:

您在尝试read_some 时遇到此异常的事实意味着 HTTP 服务器在第一个请求结束后关闭连接,即服务器忽略“Connection: keep-alive”标头(请注意 HTTP 1.0 服务器不会'不一定支持持久连接)。

但是,在 1.1 版本中,连接是 persistent by default,因此请求“HTTP/1.1”应该可以解决此问题。

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    相关资源
    最近更新 更多