提升::ASIO 文件发送

boost::asio file sending

本文关键字:ASIO 提升 文件      更新时间:2023-10-16

有一个服务器通过http响应.png文件:

#include "server.h"
string Server::header(int contentLength)
{
    string h =
    "HTTP/1.1 200 OKn"
    "Content-Length: " + boost::lexical_cast<string>(contentLength) + "n"
    "Content-Type: image/png;n"
    "Connection: closen"
    "n";
    return h;
}
string Server::readMap(const string &filename)
{
    ifstream file (filename.c_str(), ios::in|ios::binary);
    string reply;
    char buf[512];
    while (file.read(buf, sizeof(buf)).gcount() > 0)
        reply.append(buf, file.gcount());
    return reply;
}
void Server::run(const string &filename, int port)
{
    string data = readMap(filename);
    try
    {
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));
        for (;;)
        {
          tcp::socket socket(io_service);
          acceptor.accept(socket);
          boost::asio::write(socket, boost::asio::buffer(header( data.size() )));
          boost::asio::write(socket, boost::asio::buffer(data));
        }
    }
    catch (std::exception& e)
    {
        cerr << "exception: " << e.what() << endl;
    }
}

每次发生错误时:

异常:对等方重置连接

我可以在浏览器中看到图像的某些部分,有时图像几乎是完整的,但它永远不会在没有错误的情况下工作。

如果我使用 wget 它看起来像

wget http://localhost:8089
--2012-03-07 12:07:19--  http://localhost:8089/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:8089... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760032 (742K) [image/png]
Saving to: `index.html'
62% [========================================================>                                   ] 475,136     --.-K/s   in 0.002s  
2012-03-07 12:07:19 (287 MB/s) - Read error at byte 475136/760032 (Connection reset by peer). Retrying.
--2012-03-07 12:07:20--  (try: 2)  http://localhost:8089/
Connecting to localhost|127.0.0.1|:8089... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760032 (742K) [image/png]
Saving to: `index.html'
73% [==================================================================>                         ] 557,056     --.-K/s   in 0.001s  
... many failes and finally
--2012-03-07 12:09:01--  (try: 9)  http://localhost:8089/
Connecting to localhost|127.0.0.1|:8089... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760032 (742K) [image/png]
Saving to: `index.html'
100%[===========================================================================================>] 760,032     --.-K/s   in 0.001s 

有什么想法可以解决吗?

ASIO-docs 中有几个更完整的 HTTP 实现,包括静态文件服务。一种方法是为应用程序重用一些示例代码。

在这种特殊情况下,有一个示例说明如何在 http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/example/http/server/request_handler.cpp

  std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
    ...
  char buf[512];
  while (is.read(buf, sizeof(buf)).gcount() > 0)
    rep.content.append(buf, is.gcount());

这些文档还提供了实际异步HTTP实现的示例。(我假设你正在使用 boost::asio 最终使其异步?

您应该首先接收并解码 HTTP 请求,并且仅在请求的内容时才发送内容。浏览器有时也会请求其他资源;如果您发送了意外内容,或者如果您在他们发送请求之前发送了内容,他们可能会感到不安。

您似乎在数据大小中也有一个错误 - 您将data.size()-1放在标头中,然后发送所有data.也许这是 readMap 中错误的部分解决方法,您在达到 EOF 后推送一个额外的字符。你最好通过在阅读后但在推动角色之前检查eof()来解决这个问题;或者以不易出错(且更有效(的方式读取,例如:

std::copy(std::istreambuf_iterator<char>(file),
          std::istreambuf_iterator<char>(), 
          std::back_inserter(data));

另外,我认为没有任何理由将矢量复制到string中. vector也可以转换为asio::buffer

首先,您读取文件的方式不正确。

不仅仅是一次阅读一个字符不是一个好主意,而是循环是错误的。您可以使用istreambuf_iterator<char>输入或read()多个字符,gcount()确定读取何时完成。